Using Python to Read Rows of CSV Files With Column Content containing Comma
<p>I am trying to parse this CSV and print out the various columns separately.</p>

<p>However my code is having difficulty doing so possibly due to the commas in the addresses, making it hard to split them into 3 columns.</p>

<p>How can this be done?</p>

<p><strong>Code</strong></p>

<pre><code>with open("city.csv") as f:
    for row in f:
        print row.split(',')
</code></pre>

<p><strong>Result</strong></p>

<pre><code>['original address', 'latitude', 'longitude\n']
['"2 E Main St', ' Madison', ' WI 53703"', '43.074691', '-89.384168\n']
['"Minnesota State Capitol', ' St Paul', ' MN 55155"', '44.955143', '-93.102307\n']
['"500 E Capitol Ave', ' Pierre', ' SD 57501"', '44.36711', '-100.346342\n']
</code></pre>

<p><strong>city.csv</strong></p>

<pre><code>original address,latitude,longitude
"2 E Main St, Madison, WI 53703",43.074691,-89.384168
"Minnesota State Capitol, St Paul, MN 55155",44.955143,-93.102307
"500 E Capitol Ave, Pierre, SD 57501",44.36711,-100.346342
</code></pre>

<p>You should always use <code>csv</code> module</p>

<pre><code>import csv

with open("city.csv") as f:
    csv_reader = csv.reader(f):
    for row in csv_reader:
        print row
</code></pre>

python python-2.7 csv