Python列表列表

时间:2018-01-13 23:07:33

标签: python

我有一个包含此数据的列表:

[[u'Part #', u'UPC Code', u'Description', u'Avail', u'Year', u'Dealer', u'MAP', u'MSRP', u'On PO', u'Discontinued'], [u'00031300', u'887202014284', u'SE3 VISOR; TEAM NAVY', u'14', u'17M', u'18.88', u'40', u'40', u'No', u'Yes'], [u'00032200', u'887202014291', u'SE3 VISOR; CYCLOPS BLK/RED', u'31', u'17M', u'18.88', u'40', u'40', u'No', u'Yes'], [u'00032400', u'887202014307', u'SE3 VISOR; CYCLOPS RED/YEL', u'36', u'17M', u'18.88', u'40', u'40', u'No', u'Yes'], [u'00032500', u'887202014314', u'SE3 VISOR; CYCLOPS YEL/PUR', u'8', u'17M', u'18.88', u'40', u'40', u'No', u'Yes'], [u'00035200', u'887202014338', u'SE3 VISOR; PISTON BLACK', u'14', u'17M', u'18.88', u'40', u'40', u'No', u'Yes'], [u'00035300', u'887202014345', u'SE3 VISOR; PISTON BLUE', u'17', u'17M', u'18.88', u'40', u'40', u'No', u'Yes'], [u'00040700', u'887202044113', u'SE3 VISOR; TEAM ORANGE', u'17', u'17M', u'18.88', u'40', u'40', u'No', u'Yes']]

我尝试使用以下方法将其写入CSV文件:

with open('tldsearchresults.csv', 'wb') as myfile:
     wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
     wr.writerows(data)

写入的文件不是csv文件。它几乎与列表相同,只是添加了一些引号。

"[u'Part #', u'UPC Code', u'Description', u'Avail', u'Year', u'Dealer', u'MAP', u'MSRP', u'On PO', u'Discontinued']","[u'00031300', u'887202014284', u'SE3 VISOR; TEAM NAVY', u'14', u'17M', u'18.88', u'40', u'40', u'No', u'Yes']","[u'00032200', u'887202014291', u'SE3 VISOR; CYCLOPS BLK/RED', u'31', u'17M', u'18.88', u'40', u'40', u'No', u'Yes']","[u'00032400', u'887202014307', u'SE3 VISOR; CYCLOPS RED/YEL', u'36', u'17M', u'18.88', u'40', u'40', u'No', u'Yes']","[u'00032500', u'887202014314', u'SE3 VISOR; CYCLOPS YEL/PUR', u'8', u'17M', u'18.88', u'40', u'40', u'No', u'Yes']","[u'00035200', u'887202014338', u'SE3 VISOR; PISTON BLACK', u'14', u'17M', u'18.88', u'40', u'40', u'No', u'Yes']","[u'00035300', u'887202014345', u'SE3 VISOR; PISTON BLUE', u'17', u'17M', u'18.88', u'40', u'40', u'No', u'Yes']","[u'00040700', u'887202044113', u'SE3 VISOR; TEAM ORANGE', u'17', u'17M', u'18.88', u'40', u'40', u'No', u'Yes']"

我的预期输出需要如下所示:

Part #,UPC Code,Description,Avail,Year,Dealer,MAP,MSRP,On PO,Discontinued

00031300,887202014284,SE3 VISOR; TEAM NAVY,14,17M,18.88,40',40,No,Yes

1 个答案:

答案 0 :(得分:0)

我无法复制您的确切输出,但我怀疑QUOTE_ALL对您不喜欢的输出有贡献。当我运行你的代码时,这就是我得到的:

import csv
data = [[u'940489922', u'887202225253', u'BN3TH TLD UNDERWEAR; MEGABURST GRY/BLK SM', u'', u'18S', u'17.5', u'35', u'35', u'Yes', u'No'],
        [u'940489923', u'887202225260', u'BN3TH TLD UNDERWEAR; MEGABURST GRY/BLK MD', u'', u'18S', u'17.5', u'35', u'35', u'Yes', u'No'], 
        [u'940489924', u'887202225277', u'BN3TH TLD UNDERWEAR; MEGABURST GRY/BLK LG', u'', u'18S', u'17.5', u'35', u'35', u'Yes', u'No'], 
        [u'940489925', u'887202225284', u'BN3TH TLD UNDERWEAR; MEGABURST GRY/BLK XL', u'', u'18S', u'17.5', u'35', u'35', u'Yes', u'No'], 
        [u'940489926', u'887202225291', u'BN3TH TLD UNDERWEAR; MEGABURST GRY/BLK 2X', u'', u'18S', u'17.5', u'35', u'35', u'Yes', u'No'], 
        [u'942003200', u'887202229831', u'TLD MTB FENDER; BLACK OS', u'100', u'18B', u'8.25', u'15', u'15', u'No', u'No']]
with open('tldsearchresults.csv', 'wb') as myfile:
    wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
    wr.writerows(data)

其内容显示正确:

$ cat tldsearchresults.csv 
"940489922","887202225253","BN3TH TLD UNDERWEAR; MEGABURST GRY/BLK SM","","18S","17.5","35","35","Yes","No"
"940489923","887202225260","BN3TH TLD UNDERWEAR; MEGABURST GRY/BLK MD","","18S","17.5","35","35","Yes","No"
"940489924","887202225277","BN3TH TLD UNDERWEAR; MEGABURST GRY/BLK LG","","18S","17.5","35","35","Yes","No"
"940489925","887202225284","BN3TH TLD UNDERWEAR; MEGABURST GRY/BLK XL","","18S","17.5","35","35","Yes","No"
"940489926","887202225291","BN3TH TLD UNDERWEAR; MEGABURST GRY/BLK 2X","","18S","17.5","35","35","Yes","No"
"942003200","887202229831","TLD MTB FENDER; BLACK OS","100","18B","8.25","15","15","No","No"

但是,我认为你所希望的是QUOTE_MINIMAL

with open('tldsearchresults.csv', 'w') as myfile:
    wr = csv.writer(myfile, quoting=csv.QUOTE_MINIMAL)
    wr.writerows(data)

没有引号会写(除非在任何文本字段中都有逗号):

$ cat tldsearchresults.csv 
940489922,887202225253,BN3TH TLD UNDERWEAR; MEGABURST GRY/BLK SM,,18S,17.5,35,35,Yes,No
940489923,887202225260,BN3TH TLD UNDERWEAR; MEGABURST GRY/BLK MD,,18S,17.5,35,35,Yes,No
940489924,887202225277,BN3TH TLD UNDERWEAR; MEGABURST GRY/BLK LG,,18S,17.5,35,35,Yes,No
940489925,887202225284,BN3TH TLD UNDERWEAR; MEGABURST GRY/BLK XL,,18S,17.5,35,35,Yes,No
940489926,887202225291,BN3TH TLD UNDERWEAR; MEGABURST GRY/BLK 2X,,18S,17.5,35,35,Yes,No
942003200,887202229831,TLD MTB FENDER; BLACK OS,100,18B,8.25,15,15,No,No