bill
bill

Reputation: 3

Python read file and assign split list data to dictionary

I'm trying to read lines from a file and create an OrderedDict from split line data from a file but it seems that I'm only able to get the last line to output to my dictionary.

from collections import OrderedDict

city_file = open('US.txt', 'r')

cities = OrderedDict()
states = OrderedDict()

for row in city_file:
    city_data = row.split('\t')

    print(city_data)  # prints each list row of the file just fine

    state = city_data[3].strip()

    states[state] = {
        'name': state,
        'code': city_data[4].strip(),
    }

    cities[city_data[5]] = {
        'name': city_data[5].strip(),
        'state': list(states.keys()).index(state) + 1,
        'zip_code': city_data[1].strip(),
        'latitude': city_data[9].strip(),
        'longitude': city_data[10].strip()
    }

I am unable to get a dictionary of each unique state or city with their corresponding column data from the text file.

It seems the reference to the city_data list is lost when I assign it to the dictionary states. Why is that?

Each row in the file looks something like this:

US  99788   Chalkyitsik Alaska  AK  Yukon-Koyukuk (CA)  290         65.2264 -151.0251
US  36003   Autaugaville    Alabama AL  Autauga 001         32.4625 -86.7149

Edit: Clarification In my end result I'm trying to get a dictionary of states with each unique state name as the key (so the state dict should have 50 states), the cities dict should have every city name for each key. But when I perform a len(cities) the length is 1 with only the last one being stored in the dict.

Upvotes: 0

Views: 442

Answers (1)

Celeo
Celeo

Reputation: 5682

From your comment wanting OrderedDict([('Wyoming', {'name': 'Wyoming', 'code': 'WY'})]):

from collections import OrderedDict

city_file = open('US.txt', 'r')
states = OrderedDict()
cities = OrderedDict()
for row in city_file:
    data = [e.strip() for e in row.split('  ')]
    states[data[3]] = {
        'name': data[3],
        'code': data[4]
        }
    cities[data[5]] = {
        'name': data[5],
        'state': data[3],
        'zip_code': data[1],
        'latitude': data[8],
        'longitude': data[9]
    }

Output:

>>> states
OrderedDict([('AK', {'code': 'Yukon-Koyukuk (CA)', 'name': 'AK'}), ('', {'code': 'Alabama AL', 'name': ''})])
>>> cities
OrderedDict([('290', {'latitude': '', 'state': 'AK', 'name': '290', 'longitude': '65.2264 -151.0251', 'zip_code': '99788'}), ('Autauga 001', {'latitude': '', 'state': '', 'name': 'Autauga 001', 'longitude': '32.4625 -86.7149', 'zip_code': '36003'})])

Upvotes: 0

Related Questions