仅第一个和第三个元素从python列表打印到csv文件 [英] Only the first and third element printed from python list to csv file
问题描述
我正在尝试将列表写入csv文件. 我面临几个问题.
I am trying write a list to a csv file. I face several problems.
- writer = csv.writer(f)AttributeError:列表"对象没有属性作家"
我使用了此链接来解决它,并且它起作用了,并且只有打印第二个for,而没有写第一个和第三个for.
I used this link to solve it and it worked, and only printed the second for without writing the first and third 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代码结合起来,下面是代码:
The idea is to replace list that contains 1 with the list index. This problem is solved already. When I used the link above to solve the first problem, the code ran but did not write into the csv file. I tried to combine the solution from the first problem with @gumboy code and here is the code:
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)
就像我上面提到的那样,该代码可以正常工作,但是仅打印第二个即可,而无需打印第一个和第三个元素.
Like I mentioned above, the code is working but only printed second for without print the first and third element.
打印功能与在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+')
,每次都将新字符串追加到文件中.链接
要对数据进行排序
What you are doing with the f = open('H.csv','w')
is that it is write to the file but also writing over your data. What you need to do is use f =open('H.csv', 'a+')
this appends new string everytime to the file.link
To sort data use
for x in sorted(csvdict.keys()):
使用此代码,我可以将控制台上打印的内容写入文件.
With this code I was able to write to file what was printed on console.
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")
这篇关于仅第一个和第三个元素从python列表打印到csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!