I'm not used to doing things the python way yet, but I'm almost certain the following script can be condensed. I'm not looking for speed optimization here, I'm looking for more readable code. Make it slower for all I care, but what are some ways to make this look more Python-esque.
I'm simply reading in a csv file filled with zipcodes and populating a database with 23 columns. Lots of typing. I also don't like hard coding things like the index of the row. Also, multis = '%s, '*23 followed by multis = multis[:-2] feels dirty.
Looking forward to see what some of you can come up with.
#!/usr/bin/python
import csv
import MySQLdb
db = MySQLdb.connect(host="localhost", user="root", db="test")
c = db.cursor()
f_csv = 'zip-codes-database-STANDARD.csv'
csvReader = csv.reader(open(f_csv))
ziplist = []
multis = '%s, '*23
multis = multis[:-2]
for row in csvReader:
c.execute("""INSERT INTO lock_zipcodes_complete
(`zipcode`, `city`, `state`, `county`, `areacode`,
`citytype`, `city_alias_abbreviation`,
`city_alias_name`, `latitude`, `longitude`, `timezone`,
`elevation`, `county_fips`, `dst`,
`preferred_last_line_key`, `classification_code`,
`multicounty`, `state_fips`, `city_state_key`,
`city_alias_code`, `primary_record`, `city_mixed_case`,
`city_alias_mixed_case`) VALUES(""" + multis + ')',
(row[0], row[1], row[2], row[3], row[4], row[5], row[6],
row[7], row[8], row[9], row[10], row[11], row[12],
row[13], row[14], row[15], row[16], row[17], row[18],
row[19], row[20], row[21], row[22]))