本文介绍了在CSV python中添加列并枚举它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的CSV看起来像

  John,Bomb,Dawn 
3,4,5
3 ,4,5
3,4,5

我要在前面添加ID列像这样:

  ID,John,Bomb,Dawn 
1,3,4,5
2 ,3,4,5
3,3,4,5

使用枚举函数,但我不知道如何。这里是我的代码到目前为止:

  import csv 

with open(testi.csv rb')作为输入,打开('temp.csv','wb')作为输出:
reader = csv.reader(input,delimiter =',')
writer = csv.writer ,delimiter =',')

all = []
row = next(reader)
row.append('ID')
all.append )
count = 0
读取器中的行:
count + = 1
while count:
all.append(row)
row.append enumerate(reader,1))
break
writer.writerows(全部)

输出错误:

  John,Bomb,Dawn,ID 
3,4,5, ;枚举对象在0x7fb2a5728d70>
3,4,5,<枚举对象在0x1764370>
3,4,5,<枚举对象在0x17643c0>

所以ID到底,当它应该在开始,甚至做1,2,3。

解决方案

我可以建议下面的代码来解决你的问题:



import csv

打开(testi.csv,rb)作为输入,打开('temp.csv' 'wb')作为输出:
reader = csv.reader(input,delimiter =',')
writer = csv.writer(output,delimiter =',')
$ b b all = []
row = next(reader)
row.insert(0,'ID')
all.append(row)
for k,enumerate阅读器):
all.append([str(k + 1)] + row)
writer.writerows(all)

更紧凑的代码可以是:

  all = [['ID'] + next(reader)] + [[str(k + 1)] + row for k,row in enumerate(reader)] 


b $ b

UPDATE (一些解释):



您的错误枚举功能理解。 中用于循环,并且当您遍历枚举函数结果你得到元组的序列,其中第一个项目是从列表中的项目的数量,第二个是项目本身。



枚举函数return is object(),因此当您尝试将其转换为字符串时,调用 __ repr __ 魔法,将枚举对象转换为< enumerate object at ...>



c> enumerate 有助于避免循环中的其他计数器,例如您的 count + = 1 变量。


$ b b

这里还有一个很奇怪的代码:

  while count:
all.append
row.append(enumerate(reader,1))
break

部分代码永远不能执行多次。


my CSV looks like

John,Bomb,Dawn
3,4,5
3,4,5
3,4,5

I want to add ID column in front like so:

ID,John,Bomb,Dawn
1,3,4,5
2,3,4,5
3,3,4,5

using enumerate function, but I don't know how. Here's my code so far:

import csv

with open("testi.csv", 'rb') as input, open('temp.csv', 'wb') as output:
        reader = csv.reader(input, delimiter = ',')
        writer = csv.writer(output, delimiter = ',')

        all = []
        row = next(reader)
        row.append('ID')
        all.append(row)
        count = 0
        for row in reader:
                count += 1
                while count:
                        all.append(row)
                        row.append(enumerate(reader, 1))
                        break
        writer.writerows(all)

And the output comes all wrong:

John,Bomb,Dawn,ID
3,4,5,<enumerate object at 0x7fb2a5728d70>
3,4,5,<enumerate object at 0x1764370>
3,4,5,<enumerate object at 0x17643c0>

So the ID comes in the end, when it should be in the start, and it doesn't even do the 1,2,3. Some weird error comes out.

解决方案

I can suggest the code below to solve your question:

import csv

with open("testi.csv", 'rb') as input, open('temp.csv', 'wb') as output:
    reader = csv.reader(input, delimiter = ',')
    writer = csv.writer(output, delimiter = ',')

    all = []
    row = next(reader)
    row.insert(0, 'ID')
    all.append(row)
    for k, row in enumerate(reader):
        all.append([str(k+1)] + row)
    writer.writerows(all)

More compact code can be:

all = [['ID'] + next(reader)] + [[str(k+1)] + row for k, row in enumerate(reader)]

UPDATE (some explanation):

Your have wrong enumerate function understanding. enumerate should be used in for loop and when you iterate over enumerate function result you get the sequence of the tuples where first item is ordered number of item from list and the second is item itself.

But enumerate function return is object (docs) so when you try to convert it to string it call __repr__ magic method and cast enumerate object to <enumerate object at ...>.

Another words, enumerate helps to avoid additional counters in loops such as your count += 1 variable.

Also you have a very strange code here:

while count:
    all.append(row)
    row.append(enumerate(reader, 1))
    break

this part of code never can't be performed more than one time.

这篇关于在CSV python中添加列并枚举它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 00:04