【发布时间】:2020-11-16 15:49:16
【问题描述】:
我正在尝试将文本文件清理为所需的格式。为了实现这一点,我目前正在打开文件,执行某个操作并关闭它。我对其他一些操作重复相同的操作。有没有更好的方法来做到这一点。我要执行的操作是
- 打开文本文件,用新字符串替换某个字符串
- 删除上面所有的字符串直到新的字符串
- 删除文本文件中的所有选项卡
- 用逗号替换所有空格
此外,我每次都将输出写入一个新文件。我可以在执行格式化操作的同时在同一个文件中执行所有这些操作吗?
文本文件示例
ANX ANV 91 BB
cc yy the 12
okr out 1 11
temp1 temp2(a/b) temp3(t) temp4(x)
0 11 9a 1.1
1 22 9b 12
2 33 9c 4
3 44 9d 92
预期:
temp1,temp2,temp3,temp4
0,11,9a,1.1
1,22,9b,12
2,33,9c,4
3,44,9d,92
我的代码
OldString = "temp1 temp2(a/b) temp3(t) temp4(x)"
NewString = "temp1 temp2 temp3 temp4"
def myfunction():
inputFile = open("Temp1.txt",encoding="utf8")
exportFile = open('File1.txt','w',encoding="utf8")
with inputFile as f:
#Repalce old string by NewString
newText=f.read().replace(OldString,NewString)
with exportFile as f:
f.write(newText)
lines_to_write = []
tag_found = False
#Delete all extra strings upto the NewString.
#Also remove any tabs
with open('File1.txt',encoding="utf8") as in_file:
for line in in_file:
if line.strip() == NewString:
tag_found = True
if tag_found:
lines_to_write.append(line.replace('\t', ' '))
with open('File2.txt','w',encoding="utf8") as out_file:
out_file.writelines(lines_to_write)
lines_to_write = []
tag_found = False
#Replace all blank spaces with comma
with open('File2.txt',encoding="utf8") as in_file:
for line in in_file:
if line.strip() == NewString:
tag_found = True
if tag_found:
lines_to_write.append(line.replace(' ', ','))
with open('File3.txt','w',encoding="utf8") as out_file:
out_file.writelines(lines_to_write)
谢谢
【问题讨论】:
-
你能解释一下是什么问题吗?我们不应该为您做这件事,只是帮助您找到错误或其他东西......
-
嗨@adir。感谢您的关注。正如我的问题中提到的,我正在尝试改进代码。并且还找到了如何一次执行多个操作,而无需一次又一次地打开和关闭文件。
标签: python text formatting