-1

How can I convert a csv file into a space separated file in python (or using other languages or method if needed)?

So I want to have:

1,2,3,4
5,6,7,8

be converted into:

1 2 3 4
5 6 7 8

Thank you!

pumpkinjuice
  • 75
  • 1
  • 2
  • 6

2 Answers2

1

For fun, here it is in one line:

open('out.csv', 'w').write('\n'.join(map(' '.join, __import__('csv').reader(open('in.csv')))))
Alex Hall
  • 34,833
  • 5
  • 57
  • 89
0

You could read it, replace the commas with spaces and write the output to a new file:

with open ('commas.txt') as input_file:
    s = input_file.read().replace(',', ' ')
    with open ('spaces.txt', 'w') as output_file:
        output_file.write(s)

Note that such a naive implementation may cause problems if the file is very large, as it's all read to memory at once. If the file is indeed too large, a chunked approach, perhaps reading line by line would be recommended.

Mureinik
  • 297,002
  • 52
  • 306
  • 350