使用 Python 导入 CSV(查找表)并将 GPS 坐标添加到另一个输出 CSV
因此,我已经导入了一个包含 3000 个元素的 XML-ish 文件,并将它们解析为 CSV 以便输出。但我还需要导入第二个 CSV 文件,其中包含“关键字”、“纬度”、“经度”作为列,并使用它将 GPS 坐标添加到第一个文件的其他列中。
阅读 python 教程,似乎 {dictionary} 是我所需要的,尽管我在这里读到元组可能会更好。我不知道。
但无论哪种方式 - 我从以下开始:
floc = open('c:\python\kenya_location_lookup.csv','r')
l = csv.DictReader(floc)
for row in l: print row.keys()
输出看起来像:
{'LATITUDE': '-1.311467078', 'LONGITUDE': '36.77352011', 'KEYWORD': 'Kianda'} {'纬度': '-1.315288401', '经度': '36.77614331', '关键字': '索韦托'} {'纬度':'-1.315446430425027','经度':'36.78170621395111','关键字':'Gatwekera'} {'LATITUDE': '-1.3136151425171327', 'LONGITUDE': '36.785863637924194', 'KEYWORD': 'Kisumu Ndogo'}
我是新手(而不是程序员)。问题是如何使用键提取相应的行数据并将其与另一组元素主体中的单词进行匹配?
So I have already imported one XML-ish file with 3000 elements and parsed them into a CSV for output. But I also need to import a second CSV file with 'keyword','latitude','longitude' as columns and use it to add the GPS coordinates to additional columns on the first file.
Reading the python tutorial, it seems like {dictionary} is what I need, although I've read on here that tuples might be better. I don't know.
But either way - I start with:
floc = open('c:\python\kenya_location_lookup.csv','r')
l = csv.DictReader(floc)
for row in l: print row.keys()
The output look like:
{'LATITUDE': '-1.311467078', 'LONGITUDE': '36.77352011', 'KEYWORD': 'Kianda'}
{'LATITUDE': '-1.315288401', 'LONGITUDE': '36.77614331', 'KEYWORD': 'Soweto'}
{'LATITUDE': '-1.315446430425027', 'LONGITUDE': '36.78170621395111', 'KEYWORD': 'Gatwekera'}
{'LATITUDE': '-1.3136151425171327', 'LONGITUDE': '36.785863637924194', 'KEYWORD': 'Kisumu Ndogo'}
I'm a newbie (and not a programmer). Question is how do I use the keys to pluck out the corresponding row data and match it against words in the body of the element in the other set?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
他们都是完成这项任务的不错选择。
不,不是!这是
print row
的输出,绝对不是print row.keys()
。请不要在您的问题中提供虚假信息,这会使问题很难有效回答(作为新手没有什么区别:您当然可以检查您提供的输出实际上是否来自您也提供的代码!)。由于您向我们提供的有关“另一组”结构的信息绝对为零,因此您当然无法回答这个问题。疯狂猜测,例如,如果“另一组”中的条目也是
dict
,每个都有一个KEYWORD
键,那么您想先构建一个辅助字典,然后合并“其他集合”中的(某些)条目:当 CSV 中的相应关键字条目中不存在该位置时,这将使其他集合中的位置丢失 - 如果这是一个问题,您可能需要使用“假位置”字典作为缺失条目的默认值,而不是我显示的最后一条语句中的
()
。但是,由于您的问题中缺乏信息,无论如何这都是疯狂的猜测。They're both fine choices for this task.
No it doesn't! This is the output from
print row
, most definitely NOTprint row.keys()
. Please don't supply disinformation in your questions, it makes them really hard to answer effectively (being a newbie makes no difference: surely you can check that the output you provide actually comes from the code you also provide!).Since you give us absolutely zero information on the structure of "the other set", you make it of course impossible to answer this question. Guessing wildly, if for example the entries in "the other set" are also
dict
s each with a key ofKEYWORD
, you want to build an auxiliary dict first, then merge (some of) its entries in the "other set":This will leave the location missing from the other set when not present in a corresponding keyword entry in the CSV -- if that's a problem you may want to use a "fake location" dictionary as the default for missing entries instead of that
()
in the last statement I've shown. But, this is all wild speculation anyway, due to the dearth of info in your Q.如果将 DictReader 转储到列表中 (
data = [row for row in csv.DictReader(file)]
),并且每行都有唯一的关键字,则将该词典列表转换为包含字典,使用该关键字作为键。提取关键字后,您可以迭代其他列表,从中获取关键字,并将其用作纬度/经度列表的索引。从该索引中取出纬度/经度并将其添加到另一个列表中。
If you dump the DictReader into a list (
data = [row for row in csv.DictReader(file)]
), and you have unique keywords for each row, convert that list of dictionaries into a dictionary of dictionaries, using that keyword as the key.Once you have the keyword pulled out, you can iterate through your other list, grab the keyword from it, and use it as the index for the lat/long list. Pull out the lat/long from that index and add it to the other list.
谢谢 -
亚历克斯:我的另一组代码正在工作,唯一相关的部分是我有一个字符串,可能包含也可能不包含该字典中的“关键字”。
从结构上讲,这就是我组织它的方式:(
我省略了获取其标题、位置的代码,因为它们可以工作,并且不会用于从故事中的数据获取真实位置)
希望这会有所帮助。
Thanks -
Alex: My code for the other set is working, and the only relevant part is that I have a string that may or may not contain the 'keyword' that is in this dictionary.
Structurally, this is how I organized it:
(I omitted the code for getting its, titles, locations because they work and will not be used to get the real locations from the data within stories)
Hope this helps.