The easiest way is to just set:
reader.fieldnames = "email", "name", "id", "phone"
You can save the old fieldnames if you want too.
Answer from jamylak on Stack Overflowpython - Replace fieldnames when using DictReader - Stack Overflow
python - Reading column names alone in a csv file - Stack Overflow
python - Read CSV items with column name - Stack Overflow
When using reader or DictReader from the csv module, why do you need to access the return value while the file is still open?
The easiest way is to just set:
reader.fieldnames = "email", "name", "id", "phone"
You can save the old fieldnames if you want too.
Just replace this line:
reader = csv.DictReader(f, fieldnames = ( "foo","bar","foobar","barfoo" ))
with this:
reader = csv.DictReader(f, fieldnames=("id", "email", "name", "phone"))
Though you already have an accepted answer, I figured I'd add this for anyone else interested in a different solution-
- The
csvmodule'sDictReaderobject has a public attribute calledfieldnames(as of Python 2.6 and above). https://docs.python.org/3.4/library/csv.html#csv.csvreader.fieldnames
An implementation could be as follows:
import csv
with open('C:/mypath/to/csvfile.csv', 'r') as f:
dict_reader = csv.DictReader(f)
#get header fieldnames from DictReader and store in list
headers = dict_reader.fieldnames
#sample file reading logic
for line in dict_reader:
print(line[headers[0]])
In the above, dict_reader.fieldnames returns a list of your headers (assuming the headers are in the top row). Which allows...
>>> print(headers)
['MyColumn1', 'MyColumn2', 'MyColumn3']
If your headers are in, say the 2nd row (with the very top row being row 1), you could do as follows:
import csv
with open('C:/mypath/to/csvfile.csv', 'r') as f:
#you can eat the first line before creating DictReader.
#if no "fieldnames" param is passed into
#DictReader object upon creation, DictReader
#will read the upper-most line as the headers
f.readline()
dict_reader = csv.DictReader(f)
headers = dict_reader.fieldnames
#sample file reading logic
for line in dict_reader:
print(line[headers[0]])
You can read the header by using the next() function which return the next row of the reader’s iterable object as a list. then you can add the content of the file to a list.
import csv
with open("C:/path/to/.filecsv", "rb") as f:
reader = csv.reader(f)
i = reader.next()
rest = list(reader)
Now i has the column's names as a list.
print i
>>>['id', 'name', 'age', 'sex']
Also note that reader.next() does not work in python 3. Instead use the the inbuilt next() to get the first line of the csv immediately after reading like so:
import csv
with open("C:/path/to/.filecsv", "rb") as f:
reader = csv.reader(f)
i = next(reader)
print(i)
>>>['id', 'name', 'age', 'sex']
You are looking for DictReader
with open('info.csv') as f:
reader = csv.DictReader(f, delimiter=';')
for row in reader:
name = row['name']
blah = row['blah']
to quote from the link:
Create an object which operates like a regular reader but maps the information read into a dict whose keys are given by the optional fieldnames parameter. ... If the fieldnames parameter is omitted, the values in the first row of the csvfile will be used as the fieldnames.
You can use a csv.DictReader instance to get this behaviour.
Example from the docs:
>>> with open('names.csv', newline='') as csvfile:
... reader = csv.DictReader(csvfile)
... for row in reader:
... print(row['first_name'], row['last_name'])
...
Eric Idle
John Cleese
The reader generates the dictionary keys from the first row of the csv file automatically. If the csv file does not contain a header row you can set the keys by passing a list to the DictReader:
fieldnames = ['first_name', 'last_name']
reader = csv.DictReader(csvfile, fieldnames=fieldnames)