Change characters inside a .txt file with script
<p>I have a text file:<br>
<code>3.4 - New York, United States</code>     </p>

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

<p>And then save the output as a .csv file</p>

<p>Use the CSV module, here is a stackoverflow example, <a href="http://stackoverflow.com/questions/6916542/writing-list-of-strings-to-excel-csv-file-in-python">Writing List of Strings to Excel CSV File in Python</a>. If you want to replace the "-" with a "," open the file and read the string, replace it with <code>str.replace('-', ',')</code> and write it back to the file. Here is an example. </p>

<pre><code>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()
</code></pre>

python csv