사용자가 텍스트 파일에 내용을 입력하고 그 내용을 수정하는 코드를 작성하고 있습니다.
입력
command = int(input("(1. input 2. edit) >> "))
if command == 1:
pattern_input = str(input("pattern >> ")).lower()
template_input = str(input("template >> ")).lower()
pattern = "<pattern>" + pattern_input + "</pattern>"
content = ""
if ";" in template_input:
template_list = template_input.split("; ")
for i in template_list:
content += "<li>" + i + "</li>" + "\n"
template = "<template>" + "<random>" + "\n" + content + "</random>" + "</template>"
else:
content = template_input
template = "<template>" + content + "</template>"
category = "<category>" + pattern + "\n" + template + "</category>"
with open('file.txt', 'a+t') as f:
f.write(category + "\n")
print(category)
수정
- 파이썬에서 텍스트 파일을 불러옴
- 해당 파일에서 특정 문장(사용자 입력1)이 있는지 확인
- 해당 문장을 바꿀 문장(사용자 입력2) 문장으로 바꾸기
이렇게 진행을 하려고 하는데 파일을 읽어올 때
- read나 readlines중에 어떤걸 사용해야 할까요?
- 파일 입출력시 read와 write가 동시에 가능한지 ?
- 특정 문장을 찾기 위해서는
if "*" in file:
구문을 사용하면 될까요? - 문장을 바꾸기 위해서 replace 함수를 쓰면 될까요?
도와주시면 감사하겠습니다.