-1

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?

Tonechas
  • 13,398
  • 16
  • 46
  • 80
Alexandre Pedrecal
  • 117
  • 1
  • 2
  • 10

2 Answers2

13

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', ','))
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
3

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
Tonechas
  • 13,398
  • 16
  • 46
  • 80