开发者

python: how do i always start from the second row in csv?

b holds the contents of a csv file

i need to go through every row of b; however, since it has a header, i dont want to pay attention to the header. how do i start 开发者_运维知识库from the second row?

for row in b (starting from the second row!!):


Prepend a next(b) (in every recent version of Python; b.next() in older ones) to skip the first row (if b is an iterator; if it is, instead, a list, for row in b[1:]:, of course).


b.next()
for row in b:
    # do something with row

But consider using the csv module, especially with DictReader.


Easiest way is to use a DictReader. It will consume the first row for you


You can use this code:

with open('data.csv', 'a', newline='') as filecsv:
   datafile = csv.writer(filecsv)
   datafile.writerows(list)


I just got a new approach to do this by using csv.reader's line_num:

import csv
reader = csv.reader(open(myfile))
for line in reader:
    if reader.line_num == 1:
        continue
    print(line)

This code was tested under Python 3.6.8

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜