mercredi 2 décembre 2015

Embedding an if statement within csv.reader in Python

Am trying to parse out the zips for a certain state where I have the zips for all the states. Am using csv.reader to read my file with states, zips and cities.

with open(tf) as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    zips = []
    cities = []
    states = []
    for row in readCSV:
        zip = row[0]
        city = row[3]
        state = row[4]
        print(state)
        if state == 'NJ'
            zips.append(zip)
            cities.append(city)
            states.append(state)
    print(zips)

Am running into the following error

if state == 'NJ' ^ SyntaxError: invalid syntax

Aucun commentaire:

Enregistrer un commentaire