试图弄清楚如何提取字符串并将每个字符串放入新行的新文件中
我无法绕过RegEx,我在网上看到的所有内容都显示数据全部在一条线上,但是我的已经分开了。
尝试解析另一个程序的输出,它输出三行Date,Address,Name
,然后有一个换行,另外还有三行,我只需要地址。
fo = open("C:\Sampledata.txt", "r")
item = fo.readlines(
甚至还没有任何工作!
答案 0 :(得分:0)
我不太确定这是否会增加您的问题,但也许像这样:
addresses = list()
with open("file1", "r") as input:
for line in input:
if line.startswith("Address"):
addresses.append(line.strip("\n"))
编辑:或者,如果每个文件仅包含一次“地址”,则可以在检测到以“地址”开头的行后中断循环:
addresses = list()
with open("file1", "r") as input:
for line in input:
if line.startswith("Address"):
addresses.append(line.strip("\n"))
break
然后,您可以将所有地址写入新文件。
with open("newFile", "w") as outfile:
for adress in addresses:
outfile.write(adress + "\n")
答案 1 :(得分:0)
outList = []
inText = open("C:\Sampledata.txt", "r").read()
for line in inText.split("\n"):
Date,Address,Name = line.split(",")
outList .append(Address)
outText = "\n".join(outList )
open("outFile.txt","w").write(outText )