Python CsvReader Next 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17513438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:23:56  来源:igfitidea点击:

CsvReader Next function

pythoncsv

提问by Andy

i am given a csv file but i want to skip the first row of data and move on to the next. Here is my code:

我得到了一个 csv 文件,但我想跳过第一行数据并转到下一行。这是我的代码:

def read_csv(inputfile):
    return list(csv.reader(inputfile)) #<-----

def generate_xml(reader,outfile):
    root = Element('Solution')
    root.set('version','1.0')
    tree = ElementTree(root)        
    head = SubElement(root, 'DrillHoles')
    description = SubElement(head,'description')
    current_group = None
    i = 1
    for row in reader.next(): #<-----
        x1,y1,z1,x2,y2,z2,cost = row
        if current_group is None or i != current_group.text:
            current_group = SubElement(description, 'hole',{'hole_id':"%s"%i})

            collar = SubElement(current_group,'collar')
            toe = SubElement(current_group,'toe')
            cost1 = SubElement(current_group,'cost')
            collar.text = ','.join((x1,y1,z1))
            toe.text = ','.join((x2,y2,z2))
            cost1.text = cost
        i+=1
    head.set('total_holes', '%s'%i)
    indent.indent(root)
    tree.write(outfile)

As you can see, i return the csv file as a list then i pass it onto the generate_xml function. However when i run the complete program there is an

如您所见,我将 csv 文件作为列表返回,然后将其传递给 generate_xml 函数。但是,当我运行完整的程序时,有一个

error: 'list' object has no attribute 'next'

采纳答案by Martijn Pieters

You have a list, not an iterator. Just slice it instead:

你有一个list,而不是一个迭代器。只需切片即可:

for row in reader[1:]:

or skip that first row when you still have an actual csv.reader()object:

或者当您还有实际csv.reader()对象时跳过第一行:

def read_csv(inputfile):
    reader = csv.reader(inputfile)
    next(reader)
    return list(reader)

You'd be better off returning the readerobject instead of reading all rows into memory there; unless you absolutely need random access to the rows.

您最好返回reader对象而不是将所有行读入内存中;除非您绝对需要随机访问行。

You also should really use the next()functioninstead as it works for Python 2.6+ and 3, where the iterator .next()method has been renamed to .__next__().

您还应该真正使用该next()函数,因为它适用于 Python 2.6+ 和 3,其中迭代器.next()方法已重命名为.__next__().

You'd otherwise neveruse for row in reader.next(), because .next()on a csv.reader()iterator returns onerow.

否则你永远不会使用for row in reader.next(),因为.next()csv.reader()迭代器返回一个排。

回答by Yuchao Jiang

For python 2.x, the code is:

对于python 2.x,代码是:

data = []
with open('xxx.csv') as f:
    r = csv.reader(f)
    name = r.next()[1] # assume the first 2 rows are name and unit
    unit = r.next()
    for row in r:
        data.append(row)

For python 3.x, use next(r)instead of r.next()

对于 python 3.x,使用next(r)代替r.next()