Python csv reader // how to ignore enclosing char (because sometimes it's missing)
<p>I am trying to import csv data from files where sometimes the enclosing char " is missing.</p>

<p>So I have rows like this:</p>

<pre><code>"ThinkPad";"2000.00";"EUR"
"MacBookPro";"2200.00;EUR"  
# In the second row the closing " after 2200.00 is missing
# also the closing " before EUR" is missing
</code></pre>

<p>Now I am reading the csv data with this:</p>

<pre><code>csv.reader(
            codecs.open(filename, 'r', encoding='latin-1'),
            delimiter=";",
            dialect=csv.excel_tab)
</code></pre>

<p>And the data I get for the second row is this:</p>

<p>["MacBookPro", "2200.00;EUR"]</p>

<p>Aside from pre-processing my csv files with a unix command like sed and removing all closing chars " and relying on the semicolon to seperate the columns, what else can I do?</p>

<p>If you're looping through all the lines/rows of the file, you can use string's .replace() function to get rid off the quotes (if you don't need them later-on for other purposes.).</p>

<pre><code>&gt;&gt;&gt; import csv
&gt;&gt;&gt; with open('eggs.csv', 'rb') as csvfile:
...     my_file = csv.reader(codecs.open(filename, 'r', encoding='latin-1')
...       delimiter=";",
...       dialect=csv.excel_tab)
...     )
...     for row in my_file:
...         (model,price,currency) = row
...         model.replace('"','')
...         price.replace('"','')
...         currency.replace('"','')v
...         print 'Model is: %s (costs %s%s).' % (model,price,currency)
&gt;&gt;&gt; 
Model is: MacBookPro (costs 2200.00EUR).
</code></pre>

python csv