Python csv without header

You can still use your line, if you declare the headers yourself, since you know it:

with open('data.csv') as f:
    cf = csv.DictReader(f, fieldnames=['city'])
    for row in cf:
        print row['city']

For more information check csv.DictReader info in the docs.

Another option is to just use positional indexing, since you know there’s only one column:

with open('data.csv') as f:
    cf = csv.reader(f)
    for row in cf:
        print row[0]

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)