简体   繁体   中英

How to extract values from CSV file without headers?

I have a .csv file which contains one column of values (IDs). When I use csv.Dictreader, each row is a dictionary wherein the key is the first value in the column (as it uses it as the header) and value is the ID present in the row.

I can't simply skip the first row (which I could have done had the file had a header) as I need the ID from the first row too. Manually adding a header isn't an option either.

How do I extract all the IDs from such a file as a list? I'm doing the following right now:

def returnIDs(IDfile):# extract the IDs
    IDs = []
    with open(IDfile) as f:
        reader = csv.DictReader(f)
        for row in reader:
            for key, value in row.iteritems():
                IDs.append(key)
                IDs.append(value)
    return (list(set(IDs)))  # to remove the repetetive keys

But, I'm sure there's a more Pythonic way of achieving this.

If you know the names of the columns you can specify them in the call to DictReader . Then it won't use the first row as column names and you can get the ID from the row by name.

def returnIDs(IDfile):# extract the IDs
    IDs = set()
    with open(IDfile) as f:
        reader = csv.DictReader(f, fieldnames=['ID', 'other', 'fields'])
        for row in reader:
            IDs.add(row['ID'])
    return list(IDs)

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