擅长:python、mysql、java
<p>将csv行压缩到一起后,如果值包含所有数字,则可以使用列表理解将每列中的值转换为整数:</p>
<p>csv_test.csv的内容:</p>
<pre><code>a,b,c,d,e,f
1,2,3,4,5,6
7,8,9,10,11,12
</code></pre>
<p>类型转换脚本:</p>
<pre><code>import csv
with open('csv_test.csv') as ifile:
reader = csv.reader(ifile)
a,b,c,d,e,f = zip(*reader)
f_type_con = [int(i) if i.isdigit() == True else i for i in f]
print 'Here is column f, before: {}'.format(f)
print 'Here is column f, after: {}'.format(f_type_con)
</code></pre>
<p>输出:</p>
<pre><code>Here is column f, before: ('f', '6', '12')
Here is column f, after: ['f', 6, 12]
</code></pre>
<p>如您所见,“after”输出是一个列表,其中的数字已转换为整数,并且不再在其周围有引号。现在您应该能够使用xlsxwriter编写该列表,并且整数将以正确的格式引入。</p>