-2

I have a text file:
3.4 - New York, United States

I need to create a script (preferably written in python) that can change the - (dash) character to a , (comma) character,

And then save the output as a .csv file

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Adam Eliezerov
  • 201
  • 1
  • 2
  • 6

1 Answers1

0

Use the CSV module, here is a stackoverflow example, Writing List of Strings to Excel CSV File in Python. If you want to replace the "-" with a "," open the file and read the string, replace it with str.replace('-', ',') and write it back to the file. Here is an example.

import csv
file = open("file_in.txt",'r')
file_out = open("file_out.csv",'wb')
writer = csv.writer(file_out)
writer.writerow(" ".join(file.read().replace('-', ','))) # added join method. 
file.close(); file_out.close()
Community
  • 1
  • 1
Preston Hager
  • 1,514
  • 18
  • 33
  • This is the file_out.csv file I get: `3,.,4, ,",", ,N,e,w, ,Y,o,r,k,",", ,U,n,i,t,e,d, ,S,t,a,t,e,s," " ` – Adam Eliezerov Oct 05 '16 at 16:34
  • Try now, I added a join method. This should fix the problem, although you might have to split the string by comma and do it by column. Did you look at the link on "Writing List of Strings to CSV in Python"? – Preston Hager Oct 10 '16 at 05:49