#为每行添加行号
with open(r"d:\b.txt","r",encoding="utf-8") as f:
lines=f.readlines()
lines2=[line.rstrip()+"#"+str(index)+"\n" for index,line in zip(range(1,len(lines)+1),lines)]
with open(r"d:\b.txt","w",encoding="utf-8") as f:
f.readlines(lines2)
C:\Users\13238\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\13238\PycharmProjects\pythonProject\pythonProject\文本文件的读取和操作.py
Traceback (most recent call last):
File "C:\Users\13238\PycharmProjects\pythonProject\pythonProject\文本文件的读取和操作.py", line 6, in <module>
lines=f.readlines()
^^^^^^^^^^^^^
File "<frozen codecs>", line 322, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 4: invalid start byte
如何解决