行内のテキストを置き換えるために、このメソッドを作成しました。置き換えたい正しいテキストを出力しますが、ファイル内の変更を更新していません。私はPythonにまったく慣れていません。間違いを犯している場所を教えてください。
def regFind(regx, sub_text, file_name):
c_file = io.StringIO(file_name)
for line in c_file:
match = regx.search(line)
if match:
replaced_line = re.sub(regx, sub_text, line)
line = replaced_line
#print(line)
yield line
regx = re.compile(r'^MYTEXT')
sub_text = 'NewText'
a_file = open('file.txt').read()
for line in regFind(regex, sub_text, a_file):
print(line)