在python 2.7中使用.csv将一个简单列表作为列标题

时间:2017-02-25 19:11:00

标签: python csv

我有一个由五个项目组成的简单列表。 我想将此作为标题写入现有.csv的列,其中包含所有五列的数据。 问题是我无法将每个项目作为一个单元格写入列标题。另外,我需要使用Python 2.7,因为我将在IronPython中使用它(不支持Pandas / etc。 - 其他解决方案)。
下面是我的简单代码:

writefile =  r'E:\myfile.csv'
data = ['COLUMN A', 'COLUMN B', 'COLUMN C', 'COLUMN D', 'COLUMN E']
with open(writefile, 'w') as f:
    writer = csv.writer(f)
    writer.writerow(data)

结果是10列:

  

" COLUMN"," A"," COLUMN"," B"," COLUMN",&# 34; C"," COLUMN"," D"," COLUMN"," E"。

所需的结果只有五列:

  

"第一栏","栏目B","栏目C","栏目D","栏目E&# 34。

1 个答案:

答案 0 :(得分:0)

您应该让"COLUMN A","COLUMN B","COLUMN C","COLUMN D","COLUMN E" 模块为您的字符串引用:

<强>代码:

a = []
tags = post.find('div', id='catwrap')
for t in tags:
   a.push(t.get_text())
print a.join(" ")

<强>产地:

Developer Tools