<p>我有一个.txt文件,里面有这个-2.9,Gardena CA</p>
<p>我要做的是使用python脚本将该文本转换为.csv(表):</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>我在log.csv文件中得到的输出是-title,intro,tagline</p>
<p>我希望log.csv文件显示的是:</p>
<pre><code>title,intro
2.9,Gardena CA
</code></pre>