python - 如何修改文本文件的内容?
问题描述
我有一个文本文件包含以下数据
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
然后我找不到为文本文件添加此修改的正确方法。
解决方案
您正在尝试在字符串上调用 .write() 方法。
将您的第二行更改为x.write(x.read().replace(",",",,"))
并x.close()
在最后添加。
希望这可以帮助!
推荐阅读
- networking - Ansible - 在循环上使用“when”条件语句来排除匹配的接口
- python-3.x - 如何在循环中延迟
- c++ - 需要超过 16GB 的连续内存时,内存分配失败
- python - 在 Python 中按月、年和另一个变量分组(总和)
- liquid - 为什么 DotLiquid 或 Azure 逻辑应用会自动转换我的日期字段?
- java - 如何创建对象数组的副本,其中一些对象是彼此的子类?
- reactjs - 如何在 React js 中为单独的 .tsx 文件创建堆栈导航选项卡
- python - Azure Devops Rest API 授权标头不起作用
- flutter - Flutter 实时定位
- excel - 使用 Excel VBA 合并 Word 文件