简体   繁体   English

从python中的csv文件中提取列名

[英]extracting column names from a csv file in python

I have a large csv file with 100's of columns in it. 我有一个大型的csv文件,其中包含100列的列。 Currently im able to read the csv file and its particular row. 当前无法读取csv文件及其特定行。 My file is inside a zipfile and here is the code i have so far. 我的文件在zip文件中,这是我到目前为止的代码。

import os, sys, csv, zipfile

zip_file = zipfile.ZipFile('some_zip_file.zip')
f = zip_file.open('some_csv_file.csv', 'r')

for row in csv.reader(f):
    print row[1]

f.close()

But is there a way to extract only the column names from the csv file? 但是,有没有一种方法可以从csv文件中仅提取列名? Im using python 2.7 我正在使用python 2.7

next(csv.reader(f))将仅返回第一行(大概在列名所在的位置。)

Just get the first line of the file, by your code: 只需通过代码获取文件的第一行即可:

import os, sys, csv, zipfile

zip_file = zipfile.ZipFile('some_zip_file.zip')
f = zip_file.open('some_csv_file.csv', 'r')

for column in csv.reader(f).next():
    print column
    # collumn_name

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM