社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Roy W.

Roy W. 最近创建的主题
Roy W. 最近回复了
7 年前
回复了 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)