Instantly share code, notes, and snippets.
Created
April 28, 2012 17:50
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save a-r-d/2520784 to your computer and use it in GitHub Desktop.
Python script I wrote to make a menu for a website from a .CSV to a bunch of JS arrays/ JSON objects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#### OPEN CSV AND READ INTO A LIST OF LINES, TABED PIECES ### | |
def pullCSV(): | |
#fname = raw_input("What is filename, TAB DELIMITED! > ") | |
#headerLoc = input("linenumber where heading is (eg 2) > ") | |
fname = "store_list.csv" | |
f = open(fname, "r") | |
lines= [] | |
while(1): | |
line = f.readline() | |
if line == "": | |
break | |
# remove endlines | |
line = line.replace('\n', '') | |
line = line.replace('"', '') | |
splitlist = line.split('\t') | |
lines.append(splitlist) | |
return lines | |
#### MAKE THE JS ARRAY FOR STATES IN LIST ### | |
def mkStateArray(lines): | |
#print lines[1] | |
header = lines[1] | |
stateList = [] | |
i = 0 | |
for x in header: | |
if x == 'state' or x == 'State': | |
statePos = i | |
for line in lines: | |
# now state = line[i] | |
if line[i] not in stateList and line[i] != "" and line[i] != 'State' and line[i] != 'state': | |
stateList.append(line[i]) | |
i += 1 | |
stateList = sorted(stateList) | |
return stateList, statePos | |
#### MAKE THE JS ARRAY FOR STATES ## | |
def mkCityArray(lines, stateList, statePos): | |
cityDict = {} | |
header = lines[1] | |
for x in stateList: | |
#for each state: | |
cityList = [] # make a new list to hold the cities | |
for line in lines: | |
#for each line in the file: | |
if line[statePos] == x and line[statePos - 1] not in cityList: # if the state matches | |
# also remove repeats | |
cityList.append(line[statePos - 1]) # CITY IS LEFT OF STATE | |
cityList = sorted(cityList) | |
cityDict[x] = cityList | |
#print cityDict | |
return cityDict | |
#### MAKE ZIPCODES ### | |
def makeZipandStoreList(lines,cityDict,statePos): | |
## What to do here? | |
# I think I want to combine zip + store number | |
# also we want to be able to identify campaign/ manager from this | |
# cityDict: [{state : [citylist]},ect] | |
### I can fix this later to read the header, but for now this is simpler. | |
campaignPos = statePos + 4 | |
managerPos = statePos + 3 | |
DMAPos = statePos + 2 | |
zipPos = statePos + 1 | |
cityPos = statePos - 1 | |
storePos = statePos - 2 | |
zipsAndNums = {} # holds stuff for output | |
allData = [] # holds another list of all the data for a unique store | |
elements = ["Store","Zip", "DMA", "DistManager", "CampaignNum"] | |
for dict in cityDict: | |
#pull out each dictionary | |
for x in cityDict[dict]: | |
#print x, x is a city | |
tempList = [] | |
for line in lines: | |
#in each line: pull out the city | |
if x == line[cityPos] and line[cityPos] not in tempList: | |
#print line[cityPos], line[zipPos], line[storePos], line[managerPos], line[campaignPos] | |
string = "Zip: " + line[zipPos] + " Store: " + line[storePos] | |
tempList.append(string) | |
allData.append({"City": line[cityPos], "Zip": line[zipPos], "DMA": line[DMAPos], "DistManager": line[managerPos], "Store": line[storePos], "CampaignNum": line[campaignPos]}) | |
tempList = sorted(tempList) | |
zipsAndNums[x] = tempList | |
#zipsAndNums.append({x: tempList}) | |
#print zipsAndNums | |
#sort = sorted(zipsOnly) | |
return zipsAndNums, allData | |
### write the file #### | |
def writeJSFile(stateList, cityDict, zipsAndNums): | |
#open file | |
f = open('menu_dat.js', 'w') | |
#add state list array | |
ln = len(stateList) | |
stateArr = "state_array = [" | |
i = 0 | |
for x in stateList: | |
if i == (ln - 1): | |
#dont want a comma on last one | |
stateArr += '"' + x + '"' | |
else: | |
stateArr += '"' + x + '",' | |
i += 1 | |
stateArr += "]; \n" | |
f.write(stateArr) | |
print "states array appended to file" | |
################################ | |
#skip a line | |
f.write("\n") | |
ln = len(cityDict) | |
i = 0 | |
cityArr = "city_array = {" | |
for item in cityDict: | |
cityArr += '"' + item + '": [' | |
ln2 = len(cityDict[item]) | |
ii = 0 | |
for x in cityDict[item]: | |
if ii == (ln2 - 1): | |
cityArr += '"' + x + '"' | |
else: | |
cityArr += '"' + x + '",' | |
ii += 1 | |
#make sure we are not at end of list | |
if i == (ln - 1): | |
cityArr += '] \n' | |
else: | |
cityArr += '], \n' | |
i += 1 | |
cityArr += "}; \n" | |
f.write(cityArr) | |
print "city assoc array appended to file" | |
####################### | |
f.write("\n") | |
ln = len(zipsAndNums) | |
i = 0 | |
zipsArr = "zip_array = {" | |
for item in zipsAndNums: | |
zipsArr += '"' + item + '": [' | |
ln2 = len(zipsAndNums[item]) | |
ii = 0 | |
for x in zipsAndNums[item]: | |
if ii == (ln2 - 1): | |
zipsArr += '"' + x + '"' | |
else: | |
zipsArr += '"' + x + '",' | |
ii += 1 | |
#make sure we are not at end of list | |
if i == (ln - 1): | |
zipsArr += '] \n' | |
else: | |
zipsArr += '], \n' | |
i += 1 | |
zipsArr += "}; \n" | |
f.write(zipsArr) | |
print "Zips assoc array appended to file" | |
#close the file | |
f.close() | |
##### | |
def mk_store_DB(allData): | |
#save this to a file in JS format to be used later | |
f = open('db_stores.js', 'w') | |
ln = len(allData) | |
storeArr = "store_JSON = {" | |
i = 0 | |
for store in allData: | |
ln2 = len(store) | |
ii = 0 | |
storeArr += "'" + store["Store"] + "':" | |
storeArr += "{" | |
for x in store: | |
if ii == ln2 - 1: | |
storeArr += x + ': "' + store[x] + '"' | |
else: | |
storeArr += x + ': "' + store[x] + '",' | |
ii += 1 | |
if i == ln - 1: | |
storeArr += "} \n" | |
else: | |
storeArr += "}, \n" | |
i += 1 | |
storeArr += "};" | |
#print storeArr | |
f.write(storeArr) | |
print "stores appended to store list: db_stores.js" | |
f.close() | |
#### | |
def main(): | |
lines = pullCSV() | |
stateList, statePos = mkStateArray(lines) | |
cityDict = mkCityArray(lines, stateList, statePos) | |
zipsAndNums, allData = makeZipandStoreList(lines,cityDict,statePos) | |
mk_store_DB(allData) | |
writeJSFile(stateList, cityDict, zipsAndNums) | |
return 0 | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment