I have a big csv files with the following format:
CSV FILE 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 FILE 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. 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.
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. 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.
Naturally, you can send this out to a text file as you wish, simply by redirecting the output of print as you usually would.