1

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, Street number, Street Type and Street Name

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 ?

8
  • 1
    Are you familiar with common string operations in Python? Commented Jul 1, 2016 at 11:01
  • 1st question:- How can I make some lists for each one of the type:::-- It's already a list of addresses. What else do u want? Commented Jul 1, 2016 at 11:03
  • I am quite new to it but i have basic knowledge in programming so i think i could check this out and understand it .. i hope Commented Jul 1, 2016 at 11:04
  • Ok maybe i forgot to tell you guys the goal of the project is to make a basic autocomplete from words that we get from that famous .txt file. Commented Jul 1, 2016 at 11:06
  • Think through your design first. Do you really want a list of house numbers? For example, a dictionary of street names as keys each with a list of house numbers might be useful. Commented Jul 1, 2016 at 11:06

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

5 Comments

What an answer ! Thanks it really helps my understanding of this language.
Last question is : I am currently learning C and C++ and i was wondering if there was a same kind of "error handling" as in C or C++. By that I mean checking returns of open() or other functions ? Thanks a lot for help !
Use Exception handling in Python - see docs.python.org/3/tutorial/errors.html
@ThomasBeaudet: I'm glad you like my answer. Don't forget to upvote it, once you have enough points. :) Yes, Python has exception handling. See docs.python.org/3/tutorial/errors.html
Python works at a higher level than C/C++. For example, I have used with to open the file. This automatically closes the file if there is some error inside the with block. In C you'd need to do a lot more explicit error checking.
2

(EDIT)

Actually you don't need any extra libs. It is simple operation which could be done with list comprehension or generator or map function and split , strip str object methods if you could split by the single character

with open(sys.argv[1]) as f:
    lines = f.readlines() 
    split = lambda x: x.replace(',', '').split()
    addresses_lists = map(split, lines)

If you have more complex condition to split - use re module to parse a pattern you need. Definitely you need split function from re module.

4 Comments

Note that this does not strip the comma off the city name.
@PM2Ring, yep it could be processed after or with more complex lambda used with map for example.
No need, both answers helped me a lot since i'm really new to Python ! i'll just gather every possible infos i can ! Again, thanks a lot guys !
@PM2Ring, but you had a good point and more obvious syntax. That is good for studying ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.