Lucas
Lucas

Reputation: 1177

How to add a whitespace between all characters in a .txt file

In Python 2.7 I would like to know how to add a whitespace between every single character in a .txt file (very big file). I know how to do it with Emacs but is very slow.

Input example:

122212121212121
212121212121212
121212121212121

Expected output:

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

Upvotes: 1

Views: 78

Answers (1)

zondo
zondo

Reputation: 20336

Try this:

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

Upvotes: 4

Related Questions