문자열을 반환하는 열린 데이터 파일이 있기 때문에 텍스트 파일의 선택한 단어를 원하는 단어로 바꿀 수있는 방법을 알고 싶습니다.파일에서 데이터를 대체하는 방법
input ('replace this word')
print str.replace(text, input)
문자열을 반환하는 열린 데이터 파일이 있기 때문에 텍스트 파일의 선택한 단어를 원하는 단어로 바꿀 수있는 방법을 알고 싶습니다.파일에서 데이터를 대체하는 방법
input ('replace this word')
print str.replace(text, input)
old = input('word to replace ')
new = input('replacement word ')
with open('myfile.txt', 'r') as inp:
lines = inp.readlines()
with open('myfile.txt', 'w') as out:
for line in lines:
line = line.replace(old, new)
out.write(line)
단어 감사하지만 단어를 변경했지만 파일에 변경 내용을 저장하지 않습니다. – user2561600
이 파일에 쓰도록 변경되었습니다. –
당신은 파일을 연 어떻게 우리를 게재 할 수 있습니까? 당신이 대부분 올바른 길을 걷고있는 것처럼 보입니다 ... 위와 비슷하게 해본 적이 있거나 효과가 없다고 생각하는 예가 있습니까? –
파일 = open ('anyfile.txt', 'r') lines = file.readlines() print (lines) – user2561600