Convert txt to csv python script
<p>I have a .txt file with this inside - 2.9,Gardena CA</p>

<p>What I'm trying to do is convert that text into a .csv (table) using a python script: </p>

<pre><code>import csv
import itertools

with open('log.txt', 'r') as in_file:
    stripped = (line.strip() for line in in_file)
    lines = (line for line in stripped if line)
    grouped = itertools.izip(*[lines] * 3)
    with open('log.csv', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerow(('title', 'intro'))
        writer.writerows(grouped)
</code></pre>

<p>The output I get in the log.csv file is - title,intro,tagline</p>

<p>What I would want the log.csv file to show is:</p>

<pre><code>title,intro
2.9,Gardena CA
</code></pre>

<p>I suposse this is the output you need:</p>

<blockquote>
  <p>title,intro,tagline</p>
  
  <p>2.9,Gardena,CA</p>
</blockquote>

<p>It can be done with this changes to your code:</p>

<pre><code>import csv
import itertools

with open('log.txt', 'r') as in_file:
    lines = in_file.read().splitlines()
    stripped = [line.replace(","," ").split() for line in lines]
    grouped = itertools.izip(*[stripped]*1)
    with open('log.csv', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerow(('title', 'intro', 'tagline'))
        for group in grouped:
            writer.writerows(group)
</code></pre>

python csv text