私信  •  关注

Roy W.

Roy W. 最近创建的主题
Roy W. 最近回复了
5 年前
回复了 Roy W. 创建的主题 » 如何在python中跳过读取CSV文件的第一行?

因为这和我正在做的事情有关,我将在这里分享。

如果我们不确定是否有一个头,而您也不想导入嗅探器和其他东西呢?

如果您的任务是基本的,例如打印或附加到列表或数组,则可以使用If语句:

# Let's say there's 4 columns
with open('file.csv') as csvfile:
     csvreader = csv.reader(csvfile)
# read first line
     first_line = next(csvreader)
# My headers were just text. You can use any suitable conditional here
     if len(first_line) == 4:
          array.append(first_line)
# Now we'll just iterate over everything else as usual:
     for row in csvreader:
          array.append(row)