I have a file with level, equation and answer (separated by tabs), for a math game. For example:
Lvl Eq Ans
2 2*6 12
How can I replace the tab space by a comma ,
in a new file?
I have a file with level, equation and answer (separated by tabs), for a math game. For example:
Lvl Eq Ans
2 2*6 12
How can I replace the tab space by a comma ,
in a new file?
Read the lines from the old file and replace '\t'
with ','
in the new file. This should do:
with open('oldfile.txt') as fin, open('newfile.txt', 'w') as fout:
for line in fin:
fout.write(line.replace('\t', ','))
Use the str.replace()
method like this:
>>> s = '2\t2*6\t12'
>>> print s
2 2*6 12
>>> s.replace('\t', ',')
2,2*6,12