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
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
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()