使用python创建修改的txt文件
我想知道如何修改文本文件。假设我有.txt之类的文件使用python创建修改的txt文件,python,Python,我想知道如何修改文本文件。假设我有.txt之类的文件 1, yes, cool being how, are you doing see, you, you see them 我想创建一个新的/修改过的文件,其中 1 yes, cool being how are you doing see you, you see them 我尝试了以下代码 wordlist = [] with open('scores.txt') as f: wordlist = [line.split(
1, yes, cool being
how, are you doing
see, you, you see them
我想创建一个新的/修改过的文件,其中
1
yes, cool being
how
are you doing
see
you, you see them
我尝试了以下代码
wordlist = []
with open('scores.txt') as f:
wordlist = [line.split(None, 1)[0] for line in f]
print(wordlist[0]) # trying to see if the output was the way i wanted
因此,您希望在第一次出现
时拆分,
并与换行符(\n
)合并,然后再次将“段落”与换行符合并
wordlist = []
with open('scores.txt', 'r+') as f:
origlist = ['\n'.join(line.split(',', 1)) for line in f]
wordlist.append('\n'.join(origlist))
print(wordlist[0])
f.write(wordlist[0]) # write back to scores.txt file
产生
'1\n yes, cool being\n\nhow\n are you doing\n\nsee\n you, you see them'
(此解决方案建议不会按照您的要求在字符串末尾追加换行符。如果您正在运行windows,请将\n
替换为\r\n
,并在MAC上替换为\r
)因此,您希望在第一次出现时拆分,
并与换行符(\n
)并将“段落”与换行符重新合并
wordlist = []
with open('scores.txt', 'r+') as f:
origlist = ['\n'.join(line.split(',', 1)) for line in f]
wordlist.append('\n'.join(origlist))
print(wordlist[0])
f.write(wordlist[0]) # write back to scores.txt file
产生
'1\n yes, cool being\n\nhow\n are you doing\n\nsee\n you, you see them'
(此解决方案建议书不会按照您的要求在字符串末尾追加换行符。如果您正在运行windows,请将\n
替换为\r\n
,在MAC上替换为\r
)尝试:
file_in = open("input.txt","r")
file_out = open("output.txt","a")
line = file_in.readline()
while(line):
file_out.write(line[0:line.index(',')]+"\n"+line[line.index(',')+1::]+"\n\n")
line=file_in.readline()
file_in.close()
file_out.close()
尝试:
以下是一个带有一些噱头的版本:
with open('scores.txt', 'r') as fin, open('mod_scores.txt', 'w') as fout:
for line in fin:
try:
first, rest = line.split(',', 1)
fout.write(f"{first}\n{rest.strip()}\n\n")
except ValueError:
print(f"Found a line that has no separator:\n{line}")
这在一个上下文管理器中使用了两个open语句,其优点是,如果处理完成或出现错误,两个文件都将自动关闭(感谢Luciano-Ramalho:Fluent-Python)
像这样在输入文件的行上循环使用生成器概念,也就是说,您在任何时候都只需要在内存中保留一行,而不必为非常大的文件耗尽内存
try-except块捕获输入文件中任何有问题的行。例如,在测试时,我在底部添加了一条空行。Split()然后引发ValueError,这就是这里捕获的内容。在这里,错误处理由一个简单的print语句组成,但是您当然可以做其他各种事情
代码使用了“f字符串”(如果我没有弄错的话,可以从Python 3.6开始使用):它们提供了简洁且高效的文本格式。请注意{}项中的rest.strip():这可以确保下一行开头没有空白字符
以下是一个带有一些噱头的版本:
with open('scores.txt', 'r') as fin, open('mod_scores.txt', 'w') as fout:
for line in fin:
try:
first, rest = line.split(',', 1)
fout.write(f"{first}\n{rest.strip()}\n\n")
except ValueError:
print(f"Found a line that has no separator:\n{line}")
这在一个上下文管理器中使用了两个open语句,其优点是,如果处理完成或出现错误,两个文件都将自动关闭(感谢Luciano-Ramalho:Fluent-Python)
像这样在输入文件的行上循环使用生成器概念,也就是说,您在任何时候都只需要在内存中保留一行,而不必为非常大的文件耗尽内存
try-except块捕获输入文件中任何有问题的行。例如,在测试时,我在底部添加了一条空行。Split()然后引发ValueError,这就是这里捕获的内容。在这里,错误处理由一个简单的print语句组成,但是您当然可以做其他各种事情
代码使用了“f字符串”(如果我没有弄错的话,可以从Python 3.6开始使用):它们提供了简洁且高效的文本格式。请注意{}项中的rest.strip():这可以确保下一行开头没有空白字符
请不要只发布代码作为答案,还要解释代码的作用以及如何解决问题。带有解释的答案通常质量较高,并且更有可能吸引更多的投票。请不要只发布代码作为答案,还要包括解释代码的作用以及它如何解决问题。带有解释的答案通常质量更高,更容易吸引选票。