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

Export to CSV separating all chars into fields

我在编写 python 脚本时遇到了一些问题,该脚本用于审核来自我公司的许多不同应用程序的所有 CSV 文件,并且我几乎完成了概念验证带给老板,并炫耀我可以用 python 做什么,但问题是我不太理解 python 中的 CSV class....

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

['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)

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

The csv module doesn’t directly support reading and writing Unicode, but it is 8-bit-clean save for some problems with ASCII NUL characters. So you can write functions or classes that handle the encoding and decoding for you as long as you avoid encodings like UTF-16 that use NULs. UTF-8 is recommended. further information here

如果您希望每行 1 个字符串(即每行 1 个列表项)

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

问题是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"

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

我假设您希望按照字符串中出现的方式编写每一行。因此,不要使用 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