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 string (let's call it a CSV file, though it isn't
I have a big load of documents, text-files, that I want to search for
I have big issue with url-rewriting for IIS 7.0. I've written simple module for
How would you maintain the legacy applications that: Has no unit tests have big
i have a big web application running in perl CGI. It's running ok, it's
I have this big data-entry sort of page, a table kind of layout using
I have a big lump of binary data in a char[] array which I
i have some big xslt crashing iis (StackOverflowException) when loading an XslCompiledTransform while the
I have a big red button and I'm trying to use javascript to perform
I have pretty big background of .net, and I've decided that i want to

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.