Dhawal Joharapurkar
Dhawal Joharapurkar

Reputation: 15

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.

Upvotes: 0

Views: 2989

Answers (1)

babbageclunk
babbageclunk

Reputation: 8731

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)

Upvotes: 2

Related Questions