导出为CSV将所有字符分隔为字段

时间:2016-06-07 01:43:51

标签: python python-3.x csv export-to-csv

我正在编写一个python脚本,我正在编写审计来自我公司许多不同应用程序的所有CSV文件,我几乎完成了一个概念验证,带给老板,AND展示我可以用python做什么,但问题是我不太了解python中的CSV类......

以下是我的计算机信息列表的示例:

['EB-ABORTZ,True,False,False,0', 'EB-AGONCHAROVA,True,False,False,0',
 'EB-AHART-1,True,False,False,0', 'EB-AHEIDENREICH,True,False,False,0',
 'EB-ALOCKLEAR,True,False,False,0', 'EB-AMARGULIS,True,False,False,0',
 'EB-ASKLAR,True,False,False,0', 'EB-ASKLAR-1,True,False,False,0',
 'EB-ASKLAR-3,True,False,False,0', 'EB-BCHOW-1,True,False,False,0',
 'EB-BJOHNSON,True,False,False,0', 'EB-BLYLE,True,False,False,0',
 'EB-BRUSSUM,True,False,False,0', 'EB-CCLEARY,True,False,False,0',
 'EB-...]

以下是最后代码产生的例子......

"E","B","-","A","B","O","R","T","Z",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0"
"E","B","-","A","G","O","N","C","H","A","R","O","V","A",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0"
"E","B","-","A","H","A","R","T","-","1",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0"
"E","B","-","A","H","E","I","D","E","N","R","E","I","C","H",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0"
"E","B","-","A","L","O","C","K","L","E","A","R",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0"
"E","B","-","A","M","A","R","G","U","L","I","S",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0"

以下是我用来尝试将数据导出为CSV格式的方法的副本。

def collate_computers(computers):
    with open('results.csv', 'w', encoding='utf8', )as outfile:
        writer = csv.writer(outfile, quoting=csv.QUOTE_ALL, delimiter=',')
        for c in computers:
            writer.writerow(c)

3 个答案:

答案 0 :(得分:0)

按原样,您的写行方法生成逗号分隔值(.csv)

  

csv模块不直接支持读写Unicode,   但是对于ASCII NUL的一些问题,它是8位清除   字符。所以你可以编写处理函数的函数或类   只要你避免像编码一样编码和解码   使用NUL的UTF-16。建议使用UTF-8。   更多信息here

如果你想每行1个字符串(每行1个列表项)

with open('results.csv', 'w') as outfile:
    csvWriter = csv.writer(outfile)
    for c in computers:
        csvWriter.writerow([c.encode('utf-8')])

答案 1 :(得分:0)

问题是computers中的每个项都是一个字符串,而Python中的字符串是字符序列,因此writer.writerow()认为每个字符都是一个单独的字段。要解决此问题,您需要根据逗号所在的字段将每个字符串split首先放入字段:

import csv

def collate_computers(computers):
    with open('results.csv', 'w', encoding='utf8') as outfile:
        writer = csv.writer(outfile, quoting=csv.QUOTE_ALL, delimiter=',')
        for s in computers:
            fields = s.split(',')
            writer.writerow(fields)

computers = [
    "EB-ABORTZ,True,False,False,0",
    "EB-AGONCHAROVA,True,False,False,0",
    "EB-AHART-1,True,False,False,0",
    "EB-AHEIDENREICH,True,False,False,0",
    "EB-ALOCKLEAR,True,False,False,0",
    "EB-AMARGULIS,True,False,False,0"
]

collate_computers(computers)

<强> results.csv

"EB-ABORTZ","True","False","False","0"
"EB-AGONCHAROVA","True","False","False","0"
"EB-AHART-1","True","False","False","0"
"EB-AHEIDENREICH","True","False","False","0"
"EB-ALOCKLEAR","True","False","False","0"
"EB-AMARGULIS","True","False","False","0"

答案 2 :(得分:0)

CSV编写器函数writerow将列表作为输入,并将每个元素写入该行。将单个字符串传递给函数时,writer会将字符串解释为字符列表。

我假设你想要写出字符串中出现的每一行。因此,不要使用writer.writerow(c),而是使用writer.writerow(c.split(','))。这会将字符串拆分为一个列表,并用逗号分隔。

您的csv文件应如下所示:

EB-ABORTZ,True,False,False,0
EB-AGONCHAROVA,True,False,False,0
EB-AHART-1,True,False,False,0
EB-AHEIDENREICH,True,False,False,0
EB-ALOCKLEAR,True,False,False,0
EB-AMARGULIS,True,False,False,0
EB-ASKLAR,True,False,False,0
EB-ASKLAR-1,True,False,False,0
EB-ASKLAR-3,True,False,False,0
EB-BCHOW-1,True,False,False,0
EB-BJOHNSON,True,False,False,0
EB-BLYLE,True,False,False,0
EB-BRUSSUM,True,False,False,0
EB-CCLEARY,True,False,False,0