2016-03-02 51 views
1

在Python 2.7中,我想知道如何在.txt文件(非常大的文件)中的每個單個字符之間添加空格。我知道如何用Emacs做到這一點,但速度很慢。如何在.txt文件中的所有字符之間添加空格

輸入例:

122212121212121 
212121212121212 
121212121212121 

預期輸出:

1 2 2 2 1 2 1 2 1 2 1 2 1 2 1 
2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 

回答

4

嘗試這種情況:

with open(infile) as open_file: 
    with open(outfile, 'w') as write_file: 
     for line in open_file: 
      write_file.write(" ".join(line)) 
相關問題