我有一个文本文件包含以下数据
Repetition,4213-RTN-01-8 Counts BER,Microwave,Huawei-RTN-Alarms,Packet Drop,2938,Normal,Regional Operations,,,
我只需要替换,
为,,
我的代码是
x=open("D:\Work\Robotics\RTN Sheets\pandas.txt","r+") #open the file with read/write previlage
x.read().replace(",",",,").write() #read the contents and apply the replace action
然后,我找不到为文本文件添加此修改的正确方法。
读取文件后,应执行文件搜索以将文件指针重置为0,以便可以替换而不是附加文件内容:
with open("D:\Work\Robotics\RTN Sheets\pandas.txt", "r+") as file:
content = file.read()
file.seek(0)
file.write(content.replace(',', ',,'))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句