Thomas Beaudet Thomas Beaudet - 5 months ago 10
Python Question

How can i parse a txt file into lists in Python

I am not far from a new bee in Python and i would like to parse a file
as is :

Paris, 458 boulevard Saint-Germain
Paris, 343 boulevard Saint-Germain
Marseille, 343 boulevard Camille Flammarion
Marseille, 29 rue Camille Desmoulins
Marseille, 1 chemin des Aubagnens


The file contains : City, number streetType streetName

This order is always the same and the City is followed by a comma.

I had done this for now:

#!/usr/bin/python3.4

import readline
import sys

try:
f = open(sys.argv[1])
except:
sys.exit()

lines = f.readlines()
print(lines)


And i get this output:

['Paris, 458 boulevard Saint-Germain\n', 'Paris, 343 boulevard Saint-Germain\n', 'Marseille, 343 boulevard Camille Flammarion\n', 'Marseille, 29 rue Camille Desmoulins\n', 'Marseille, 1 chemin des Aubagnens\n']


Seems to be the thing to do but now i have 2 questions:


  1. How can I make some lists for each one of the type (City, number, streetType, streetName) ?

  2. Is there a librairy in Python that parses adresses in a list ? One that you would recommend ?


Answer

There are various data structures that you could store this data in. You could store the data from each line in a tuple containing (city_name, street_number, street_type, street_name), and then store each of those tuples into a list. A slightly nicer option is to store the data into a list of dictionaries. Here's a short demo.

fname = 'citydata.txt'

addresses = []
keys = ('city', 'num', 'type', 'name')

with open(fname) as f:
    for line in f:
        line = line.rstrip()
        city, line = line.split(',', 1)
        num, street_type, street_name = line.split(None, 2)
        t = (city, num, street_type, street_name)
        print(t)
        addresses.append(dict(zip(keys, t)))

print()
for row in addresses:
    print(row)

print()
for row in addresses:
    print(row['num'])

output

('Paris', '458', 'boulevard', 'Saint-Germain')
('Paris', '343', 'boulevard', 'Saint-Germain')
('Marseille', '343', 'boulevard', 'Camille Flammarion')
('Marseille', '29', 'rue', 'Camille Desmoulins')
('Marseille', '1', 'chemin', 'des Aubagnens')

{'city': 'Paris', 'num': '458', 'type': 'boulevard', 'name': 'Saint-Germain'}
{'city': 'Paris', 'num': '343', 'type': 'boulevard', 'name': 'Saint-Germain'}
{'city': 'Marseille', 'num': '343', 'type': 'boulevard', 'name': 'Camille Flammarion'}
{'city': 'Marseille', 'num': '29', 'type': 'rue', 'name': 'Camille Desmoulins'}
{'city': 'Marseille', 'num': '1', 'type': 'chemin', 'name': 'des Aubagnens'}

458
343
343
29
1