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

只有第一个和第三个元素从python列表打印到csv文件,python,list,loops,csv,Python,List,Loops,Csv,我正在尝试将列表写入csv文件。 我面临几个问题 writer=csv。writer(f)AttributeError:“list”对象没有属性“writer” 我用这个来解决它,它工作了,只打印了第二个,没有写第一个和第三个 这是我写的代码 其思想是用列表索引替换包含1的列表。这个问题已经解决了。当我使用上面的链接解决第一个问题时,代码运行了,但没有写入csv文件。 我尝试将第一个问题的解决方案与@gumboy代码结合起来,代码如下: csvdict = {words[0]:words[1:]

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

  • writer=csv。writer(f)AttributeError:“list”对象没有属性“writer”
  • 我用这个来解决它,它工作了,只打印了第二个,没有写第一个和第三个

    这是我写的代码

    其思想是用列表索引替换包含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文件中写入的内容: 打印:

    csf文件:

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

    使用
    f=open('H.csv','w')
    所做的是,它既可以写入文件,也可以写入数据。您需要做的是使用
    f=open('H.csv','a+')
    这会在每次文件中追加新字符串。 对数据使用进行排序

    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")
    

    问题是,对于for循环的每次迭代,您都会再次打开该文件(只打开一次),然后覆盖它,因为您将
    'w'
    作为'mode'参数传递(如果您只想向该文件附加一些内容,则可以传递'a')

    实际上您应该做的是,导入
    csv
    模块,用
    with
    语句打开文件,创建一个writer
    writer=csv.writer(csv_文件)
    ,写入头,然后在for循环中写入行。(另外,重命名
    csv
    列表,因为
    csv
    是模块的名称。)

    生成的csv文件如下所示(第一列包含客户,第二列包含产品):


    是的,这很有趣,我没有注意到是因为他们打印第二个元素,而不是第三个元素。那么把列表转换成str是正确的做法吗,先生?另一件事是为什么它不会打印最后一个(第三个for)元素?您想要的csv输出是什么?CMIIW,他们会写他们打印正确的内容吗?我的意思是,我只是想澄清我在做什么,上面没有滥用循环逻辑。既然打印和写hasilstr,他们应该写什么打印正确?我想他们会打印:costummer,1,buy,[12,22],costummer,2,buy,[8,12,38,46],costummer,3,buy,[4,34,43],但他们只打印了客户1和2。这对你有帮助吗?我的电脑一定有问题,因为我100%复制了你的代码,它打印了3个客户,但仍然只打印了csv中的2个客户。谢谢你,先生,我只是好奇,我们能把“”去掉吗?我尝试删除已阻止的列表和“”到目前为止,我使用的代码如下所示=products1=“,”。加入(str(x)for x in products)writerow((customer,products1))并删除已阻止的列表,但仍知道如何删除“”csv编写器必须添加引号,因为逗号用作分隔符,并且列表也使用逗号分隔项目。可以通过传递不同的分隔符(如分号)来防止这种情况。我已经编辑了这个例子。谢谢,先生。不客气。另外,似乎将数据存储在文件中可能是更好的选择。谢谢您,先生,下次我将尝试使用json文件。
    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")
    
    import csv
    
    
    lst = [...]  # Your csv list.
    csvdict = {words[0]:words[1:] for words in lst}
    
    with open('temp.csv', 'w', newline='') as csv_file:
        writer = csv.writer(csv_file, delimiter=';')
        writer.writerow(('costumer', 'buy'))  # Write the header.
        for costumer in csvdict:
            products = [index for index, v in enumerate(csvdict[costumer], 1) if v == '1']
            writer.writerow((costumer, products))
    
    costumer;buy
    3;[4, 34, 43]
    2;[8, 12, 38, 46]
    1;[12, 22]