从坐标创建字典的脚本? [英] Script for creating dictionary from coordinates?

查看:134
本文介绍了从坐标创建字典的脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是新的,这是我的第一篇文章。

New here, and this is my first post.

我有一个我正在处理的python脚本,主要目的是列出城市从我的计算机上的.txt文件,并且脚本吐出一个字典,其中键是城市名称,值是作为点对象的位置。此外,我必须使用这个点对象和新文件位置的字典,并将新值写入具有行数据(文本字典中的坐标旁边的城市名称)的文本文件。

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).

在过去的两个星期里,我已经花了约30个小时的时间,还没有运气让这个工作充分。现在,城市名称和坐标将在python shell中打印出来,只有城市名称将打印到文本文件中,但是我无法将一个字典中的城市名称和坐标组合到一个字典中,以打印出一个文本文件。

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.

我正在使用一个名为locations.pyc的python模块,该模块的目的是出去互联网,到Google服务器,然后引入与列表中的城市名称相关联的坐标。城市都在阿拉斯加州。

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

这里是到位置的链接。用于运行脚本的pyc模块。
location.pyc

Here is the link to the locations.pyc module that is used to run the script. location.pyc

推荐答案

当前版本的 writeDictFile 将失败,当您传递当前的大字典时会出现错误:

Your current version of writeDictFile will fail with an error when you pass it your current big dictionary:


TypeError:writelines()参数必须是一串字符串

TypeError: writelines() argument must be a sequence of strings

为了解决这个问题,你可以做几件事:

To solve it, you could do several things:


  1. 手动迭代键值在dict中配对并将其手动写入文件:

  1. 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))


  • 使用 csv模块为你做的工作。但是,在这种情况下,您需要将单个大字典转换为小字典列表:

  • 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)
    


  • 顺便说一句,python编码约定建议不要使用camelCase作为函数名。如果您有兴趣,请查看 PEP8

    By the way, python coding conventions suggest not to use camelCase for function names. Have a look at PEP8 if you're interested.

    这篇关于从坐标创建字典的脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆