我有一个文件,其中每个单词都用单个空格或制表符或多个空格分隔:
例如
address1.txt:
Bob lives in Boston
Sam lives in Houston
Jay lives in Ruston
Bill lives in Atlanta
我想将文件另存为address2.txt,其中每个单词都用制表符分隔。
我如何使用Python做到这一点?
有什么帮助吗?
谢谢里约
用于split
在空格上分割,然后join
将单词和制表符放回去。
with open('address1.txt') as fin, open('address2.txt','w') as fout:
for line in fin:
fout.write( "\t".join(line.split()) + "\n" )
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句