gzip file with splitting the record into columns when the one of the column value in double quote
<p>I have gzip file which contains columns separated by comma, but when the column value is within double quotes the commas should be kept as it is. I wrote the following code:</p>

<pre><code>                           input = gzip.open(file, "rb")
                            reader = codecs.getreader("utf-8")
                            input_file = reader(input)
                            try:
                                count = 0
                                for line in input_file:

                                    try:
                                        # print 'count='
                                        # print count
                                        if len(line) != 0:

                                            col = line.split(',')
</code></pre>

<p>My data in the file looks like:</p>

<pre><code>4798151,1137351,nam_p0,2762913,nam_r000,"NAM_Rack, Power &amp; Cooling",3
4798151,1135623,nam_s0,2762914,nam_a0,"NAM_Advise, Transform &amp; Manage",3
</code></pre>

<p>When I was splitting  data with comman, the comma with in double quotes should ignore and come into a column. I am not sure how to add the condition treating the text enclosed in double quote as one. A quick response would be a great help.
Thanks.</p>

<p>Use <a href="https://docs.python.org/2/library/csv.html" rel="nofollow">csv</a>.</p>

<p><strong>Demo</strong></p>

<pre><code>&gt;&gt;&gt; import StringIO
&gt;&gt;&gt; import csv
&gt;&gt;&gt; line = '4798151,1137351,nam_p0,2762913,nam_r000,"NAM_Rack, Power &amp; Cooling",3'
&gt;&gt;&gt; handler = StringIO.StringIO(line)
&gt;&gt;&gt; [row for row in csv.reader(handler, delimiter=',')]
[['4798151', '1137351', 'nam_p0', '2762913', 'nam_r000', 'NAM_Rack, Power &amp; Cooling', '3']]
</code></pre>

<p>In this case you can use this <em>direct approach</em>:</p>

<pre><code>with gzip.open(file, 'rb') as handler:
    for row in csv.reader(handler, delimiter=","):
        # row processing HERE
</code></pre>

python python-2.7