问题
New here, and this is my first post.
I have a python script that I am working on, and the scripts main purpose is to take a list of cities from a .txt file on my computer, and have the script spit out a dictionary where the keys are the city names and the values are the locations as point objects. Also, I have to take this dictionary of points objects and the new file location and write the new values to a text file with the data in rows (city names next to the coordinates in the dictionary).
I have literally spend about 30 hours on this over the past 2 weeks, and still have had no luck getting this to work fully. Right now, the city names and the coordinates will print out in the python shell, and just the city names will print out to the text file, but I cannot get the city names and the coordinates combined in one dictionary to print out into one text file.
I am using a python module called locations.pyc and this module has the purpose of going out onto the internet, to a Google server, and then bringing in the coordinates associated with the city names in the list. The cities are all in Alaska..
This is the script so far.
import location # calls the location module in file
def getFileList(path):
f = open(path, "r")
f.readline()
fileList = [] # removes column header
for line in f:
fileList.append(line.strip())
f.close()
return fileList
class Point:
def __init__(self, x = 0, y = 0):
self.x = float(x)
self.y = float(y)
def makeCitiesDict(citiesList):
CitiesDict = dict()
for city in citiesList:
loc = location.getaddresslocation(city)
x = loc[0]
y = loc[1]
CitiesDict[city] = Point(x, y)
return CitiesDict
def writeDictFile(aDict):
txt_file = open(r"Alaska.txt",'w')
txt_file.writelines(myCitiesDict)
txt_file.close()
myCities = getFileList(r"cities_Small.txt")
myCitiesDict = makeCitiesDict(myCities)
writeDictFile("myCitiesDict\n")
print myCitiesDict
for key in myCitiesDict:
point = myCitiesDict[key]
print point.x,point.y
Here is the link to the locations.pyc module that is used to run the script. location.pyc
回答1:
Your current version of writeDictFile
will fail with an error when you pass it your current big dictionary:
TypeError: writelines() argument must be a sequence of strings
To solve it, you could do several things:
Iterate manually over the key-value pairs in the dict and write them manually to the file:
def write_to_file(d): with open(outputfile, 'w') as f: for key, value in d.items(): f.write('{}\t{}\t{}\n'.format(key, value.x, value.y))
Use the csv module to do the work for you. However, in that case you'll want to convert your single big dictionary to a list of small dictionaries:
def makeCitiesDict(citiesList): citylist = [] for city in citiesList: loc = location.getaddresslocation(city) x = loc[0] y = loc[1] citylist.append({'cityname': city, 'lon': x, 'lat': y}) return citylist def writeDictFile(biglist): with open(outputfile, 'w') as f: dw = csv.DictWriter(f, fieldnames=('lon', 'lat', 'cityname'), delimiter='\t') dw.writerows(biglist)
By the way, python coding conventions suggest not to use camelCase for function names. Have a look at PEP8 if you're interested.
来源:https://stackoverflow.com/questions/28650264/script-for-creating-dictionary-from-coordinates