Python 在for循环中保存文本文件
我试着循环一个文件,把句子分成几行,然后导出数据Python 在for循环中保存文本文件,python,Python,我试着循环一个文件,把句子分成几行,然后导出数据 filename = '00000BF8_ar.txt' with open(filename, mode="r") as outfile: str_output = outfile.readlines() str_output = ''.join(str_output) sentenceSplit = filter(None, str_output.split(".")) for s in senten
filename = '00000BF8_ar.txt'
with open(filename, mode="r") as outfile:
str_output = outfile.readlines()
str_output = ''.join(str_output)
sentenceSplit = filter(None, str_output.split("."))
for s in sentenceSplit:
print(s.strip() + ".")
#output += s
myfile = open(filename, 'w')
myfile.writelines(s)
myfile.close()
不幸的是,循环看起来只经过几行并保存它们。因此,整个文件不会循环并保存。关于如何修复该问题的任何帮助?每次使用
'w'
选项重新打开文件时,基本上都会删除其内容
尝试按如下方式修改代码:
filename = '00000BF8_ar.txt'
with open(filename, "r") as infile:
str_output = infile.readlines()
str_output = ''.join(str_output)
sentenceSplit = filter(None, str_output.split("."))
with open(filename, "w") as outfile:
for s in sentenceSplit:
print(s.strip() + ".")
#output += s
s.writelines(s)
实现同样目的的另一种方法是使用
open(filename_new,'a')
打开一个新文件,它打开一个文件进行附加,但根据经验法则,不要在循环中打开/关闭文件。每次使用'w'
选项重新打开文件时,基本上都会删除其内容
尝试按如下方式修改代码:
filename = '00000BF8_ar.txt'
with open(filename, "r") as infile:
str_output = infile.readlines()
str_output = ''.join(str_output)
sentenceSplit = filter(None, str_output.split("."))
with open(filename, "w") as outfile:
for s in sentenceSplit:
print(s.strip() + ".")
#output += s
s.writelines(s)
实现同样目的的另一种方法是使用打开(filename_new,'a')打开一个新文件,它打开一个文件进行追加,但根据经验法则,不要在循环中打开/关闭文件。open(filename,'w')
每次启动时都会覆盖该文件。我的猜测是,目前发生的情况是,sentenceSplit
中只有最后一个元素出现在myfile
中
简单的“解决方案”是使用append
而不是write
:
open(filename, 'a')
只需在文件末尾开始写入,而不删除其余部分
然而,正如@chepner的评论所述,为什么要重新打开该文件?我建议您将代码更改为:
with open(filename, mode="r") as outfile:
str_output = outfile.readlines()
str_output = ''.join(str_output)
sentenceSplit = filter(None, str_output.split("."))
with open(filename, mode='w') as myfile:
for s in sentenceSplit:
print(s.strip() + ".")
myfile.writelines(s)
这样,您就不用多次打开它,也不用每次都覆盖它,而是只打开一次,然后不断地写入 open(filename,'w')
将在每次启动时覆盖该文件。我的猜测是,目前发生的情况是,sentenceSplit
中只有最后一个元素出现在myfile
中
简单的“解决方案”是使用append
而不是write
:
open(filename, 'a')
只需在文件末尾开始写入,而不删除其余部分
然而,正如@chepner的评论所述,为什么要重新打开该文件?我建议您将代码更改为:
with open(filename, mode="r") as outfile:
str_output = outfile.readlines()
str_output = ''.join(str_output)
sentenceSplit = filter(None, str_output.split("."))
with open(filename, mode='w') as myfile:
for s in sentenceSplit:
print(s.strip() + ".")
myfile.writelines(s)
这样,您就不用多次打开它,也不用每次都覆盖它,而是只打开一次,然后不断地写入 这是代码,我希望这是您想要实现的
filename = '00000BF8_ar.txt'
with open(filename, mode="r") as outfile:
str_output = outfile.readlines()
str_output = ''.join(str_output)
sentenceSplit = filter(None, str_output.split("."))
l=[]
for s in sentenceSplit:
l.append(s.strip() + ".")
myfile = open(filename, 'w')
myfile.write('\n'.join(l))
myfile.close()
这是代码我希望这是你想要实现的
filename = '00000BF8_ar.txt'
with open(filename, mode="r") as outfile:
str_output = outfile.readlines()
str_output = ''.join(str_output)
sentenceSplit = filter(None, str_output.split("."))
l=[]
for s in sentenceSplit:
l.append(s.strip() + ".")
myfile = open(filename, 'w')
myfile.write('\n'.join(l))
myfile.close()
循环的可能副本会在每次打开文件时截断该文件。为什么要重新打开文件,而不是在循环之前打开一次?如果使用readlines(),则读取文件中的所有行并将其保存在列表中;如果要逐行读取,则使用readline(),或者读取所有内容,然后使列表上的循环重复,则循环每次打开时都会截断文件。为什么要重新打开文件,而不是在循环之前打开一次?如果使用readlines(),它将读取文件中的所有行并将它们保存在列表中,如果要逐行读取,请使用readline(),或者读取所有内容,然后在列表上进行循环