我需要打开一个文件来读取每一行,然后在每行的末尾添加一个字符串。 然后,回写新行打开的文件。如何读取和添加stirng并在Python中写回每行?
number = 0
with open("test_openFile.txt", "r+b") as the_file:
for line in the_file:
print (line)
type(line)
line = line.strip('\n') + "\t" + str(number)
number += 1
the_file.write(line)
但是,我得到:
line = line.strip('\n')
TypeError: Type str doesn't support the buffer API
任何帮助,将不胜感激!
你有意在二进制模式下打开它吗? – TerryA