Skip to main content
 首页 » 编程设计

python之只有第一个和第三个元素从 python 列表打印到 csv 文件

2024年10月01日6yyy_WW

我正在尝试将列表写入 csv 文件。 我面临几个问题。

  1. writer = csv.writer(f) AttributeError: 'list' 对象没有属性 'writer'

我用了这个link解决它并且它起作用了,只打印了第二个 for 而没有写第一个和第三个 for。

这是@gumboy写的代码

csv = [['1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['2', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0'], ['3', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0']] 
 
csvdict = {words[0]:words[1:] for words in csv} 
for x in csvdict.keys(): # iterate over the keys '1', '2', .... 
    products = [index+1 for index,v in enumerate(csvdict[x]) if v == '1' ] # create list of purchases where the '1's are indexed 
 
    print ("costummer", x, "buy", products) 

想法是用列表索引替换包含 1 的列表。这个问题已经解决了。当我使用上面的链接解决第一个问题时,代码运行但没有写入 csv 文件。 我尝试将第一个问题的解决方案与@gumboy 代码结合起来,代码如下:

csvdict = {words[0]:words[1:] for words in csv} 
for x in csvdict.keys(): # iterate over the keys '1', '2', .... 
    products = [index+1 for index,v in enumerate(csvdict[x]) if v == '1' ] # create list of purchases where the '1's are indexed 
 
    #print (products) 
    f = open("H.csv", 'w') 
    hasil = ("costummer", x, "buy", products) 
    hasilstr = (','.join([str(x) for x in hasil])) 
    print (hasilstr) 
    f.write(hasilstr) 

就像我上面提到的,代码是有效的,但只打印了第二个元素,没有打印第一个和第三个元素。

打印函数与 csv 文件中写入的内容: 打印:

costummer,1,buy,[12, 22]  
costummer,2,buy,[8, 12, 38, 46] 
costummer,3,buy,[4, 34, 43] 

csf文件:

costummer,2,buy,[8, 12, 38, 46] 

请您参考如下方法:

您对 f = open('H.csv','w') 所做的是写入文件,同时也覆盖您的数据。您需要做的是使用 f =open('H.csv', 'a+') 这每次都会将新字符串附加到文件中。 link 排序数据使用

for x in sorted(csvdict.keys()): 

通过这段代码,我能够将打印在控制台上的内容写入文件。

csvfile = [['1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['2', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0'], ['3', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0']] 
csvdict = {words[0]:words[1:] for words in csvfile} 
for x in sorted(csvdict.keys()): # iterate over the keys '1', '2', .... 
    products = [index+1 for index,v in enumerate(csvdict[x]) if v == '1' ] # create list of purchases where the '1's are indexed 
 
    #print (products) 
    f = open("H.csv", 'a+') 
    hasil = ("costummer", x, "buy", products) 
    hasilstr = ("costummer, %s,buy,"+','.join([str(i) for i in products])) %(x) 
    print (hasilstr) 
    f.write(hasilstr +"\n")