Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6200685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:24:50+00:00 2026-05-24T04:24:50+00:00

I have a big csv files with the following format: CSV FILE 1 id,

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:24:53+00:00Added an answer on May 24, 2026 at 4:24 am

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a big csv file with datetime and value recorded every 10 seconds.
I have a big csv file which lists connections between nodes in a graph.
I have quite a big network in a CSV file. It containt 450k nodes
I have a big csv file ( about 30M ), that I want to
I have a big csv file where the first column is the day of
http://img32.imageshack.us/img32/6649/workspace1001.png big version I have this product data in a csv file, but some
I have some big .csv files and I am trying to put it into
I'm looking to compare two big sets of csv files and/or a csv file
I have 2 big CSV file with millions of rows. Because those 2 CSVs
I have an quite big CSv File I want to have in Google Maps

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.