首页 > 解决方案 > 如何修改文本文件的内容?

问题描述

我有一个文本文件包含以下数据

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

然后我找不到为文本文件添加此修改的正确方法。

标签: python

解决方案


您正在尝试在字符串上调用 .write() 方法。

将您的第二行更改为x.write(x.read().replace(",",",,"))x.close()在最后添加。

希望这可以帮助!


推荐阅读