简体   繁体   English

如何在python中搜索字典中的值

[英]How to search values in a dictionary in python

I have a big csv files with the following format: 我有一个大的csv文件,格式如下:

CSV FILE 1 CSV文件1

id, person,   city
1,   John,     NY
2,   Lucy,    Miami
3,   Smith,   Los Angeles
4,   Mike,    Chicago
5,   David,   Los Angeles
6,   Daniel,    NY

On another CSV file I have each city with a numerical code: 在另一个CSV文件中,每个城市都有一个数字代码:

CSV FILE 2 CSV文件2

city , code
NY   ,  100
Miami,  101
Los Angeles, 102
Chicago, 103

What I need to do is go through CSV File 1 in the city column, read the name of the city and get the numerical code for that city from CSV File 2. I could then just output that list of city codes to a text file. 我需要做的是通过城市列中的CSV文件1,读取城市的名称,并从CSV文件2获取该城市的数字代码。然后,我可以将城市代码列表输出到文本文件。 For this example I would get this result: 对于这个例子,我会得到这个结果:

100
101
102
103
102
100

I used csv.DictReader to create dictionaries for each file but I am stuck trying to find a way to map each city to each code. 我使用csv.DictReader为每个文件创建字典,但我试图找到一种方法将每个城市映射到每个代码。

Any ideas or pointers in the right direction would be appreciated! 任何想法或指示正确的方向将不胜感激!

You have some extra whitespace there, and unlike some storage formats, CSV does care about it. 你有一些额外的空格,与一些存储格式不同,CSV确实关心它。 If that is actually in your source data, you may have to strip it out before it will be processed as you expect (otherwise various fields will have leading and trailing whitespace). 如果它实际上在您的源数据中,您可能必须在它按预期处理之前将其删除(否则各个字段将具有前导和尾随空格)。

Assuming that the whitespace is gone, however, it's fairly straightforward to do. 然而,假设空白消失了,那么它就相当简单了。 You can just create a dictionary mapping names to codes, based on the contents of your second file. 您可以根据第二个文件的内容创建一个将名称映射到代码的字典。

from csv import DictReader

city_codes = {}
for row in DictReader(open('file2.csv', 'rb')):
    city_codes[row['city']] = row['code']

for row in DictReader(open('file1.csv', 'rb')):
    print city_codes[row['city']]

Naturally, you can send this out to a text file as you wish, simply by redirecting the output of print as you usually would. 当然,您可以根据需要将其发送到文本文件,只需像往常一样重定向打印输出即可。

除了Jeremy建议的内容之外,您还可以使用字符串方法.strip()自动删除尾随和前导空格。

Consider using sqlite3. 考虑使用sqlite3。 You can then do efficient, simple and powerful joins. 然后,您可以执行高效,简单和强大的连接。 If files are really big, you can benefit from creating proper index. 如果文件非常大,您可以从创建正确的索引中受益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM