简体   繁体   中英

How to create list using first row of CSV file in python

I'm trying to create a list out of the first row - the column headers - of a csv file using python. I put together this little script, but it prints two different lists. The first item printed is the first row of the csv and the second thing printed in the second row.

What am I doing wrong?

import csv
import sys

with open('agentsFullOutput.csv') as csvFile:
    reader = csv.reader(csvFile)
    print csvFile.next()
    field_names_list = []
    field_names_list = csvFile.next()
    print field_names_list

Every time you call .next() it'll move on to the next row in the file. So you'll only get the headers of the CSV file in the first .next() call:

import csv

with open('agentsFullOutput.csv') as csvFile:
    reader = csv.reader(csvFile)
    field_names_list = reader.next()

Any subsequent .next() call will read the next row in the file so that would be a row of data.

Each time you call next() the next line from the file is yielded until the end of the file is reached.

In your code example, since you call next() twice, and in the second call you assign it to field_name_list , it assigns the 2nd row, not the first.

The following will assign the first row to the variable field_names_list .

with open('agentsFullOutput.csv') as csvFile:
    reader = csv.reader(csvFile)
    field_names_list = next(reader)

Using next(reader) instead of reader.next() means that the code is portable to Python 3. On the 3+ series, the iterator's next() method has been renamed to __next__() for consistency.

You can use the Pandas library to read the first few lines from the huge dataset.

import pandas as pd
data = pd.read_csv("names.csv", nrows=1)

You can mention the number of lines to be read in the nrows parameter.

        with open(__csvfile) as csvFile:
            reader = csv.DictReader(csvFile)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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