python输出列表的第三项元素_仅第一个和第三个元素从python列表打印到csv文件

I am trying write a list to a csv file.

I face several problems.

writer = csv.writer(f) AttributeError: 'list' object has no attribute 'writer'

I used this link to solve it and it worked, and only printed the second for without writing the first and third for.

this is the code writen by @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)

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.

print function vs what is written on csv file:

print :

costummer,1,buy,[12, 22]

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

costummer,3,buy,[4, 34, 43]

csf file :

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

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值