3

I have file in the following format:

Berlin, Germany 
New Delhi , India
New York , USA 
Mumbai , India
Seattle, USA

I need to parse the file and print the output as

Germany : Berlin
India: New Delhi , Mumbai 
USA:  New York, Seattle 

I wrote a code:

enter code here:

def check():
    datafile=open('logfile.py','rU')
    found=False
    for line in datafile:
        if 'India' in line:
           lines=line.split()
           print("India"+":"+lines[0])
        if 'Germany' in line:
           lines=line.split()
           print("Germany"+":"+lines[0])
        if 'USA' in line:
           lines=line.split()
           print("USA"+":"+lines[0])
    datafile.close()
check()

This code is giving output as:

Germany:Berlin
India:NewDelhi
USA:NewYork
India:Mumbai
USA:Seattle

Please help.

4
  • 1
    How are you doing a default split() and losing the commas? Commented Apr 20, 2016 at 21:22
  • Essentially, as you can see in the first answer, you'll have to make lists for all countries. So the trick is to start a country list, then decide whether a country already exists or not, then append the city to the country list. Commented Apr 20, 2016 at 21:28
  • @roadrunner66 - collections.defaultdict manages all of that for you Commented Apr 20, 2016 at 22:12
  • @PaulMcGuire Ty :) Commented Apr 20, 2016 at 23:48

2 Answers 2

4

Another approach, is using defaultdict from collections to achieve this:

from collections import defaultdict

def check():
    d = defaultdict(list)
    with open('logfile.py', 'rU') as datafile:
        for line in datafile:
            data = line.split(',')
            d[data[1].strip()].append(data[0].strip())
    return d
res = check()

for k, v in res.items():
    print("{} : {}".format(k, ', '.join(v)))

Output:

India : New Delhi, Mumbai
Germany : Berlin
USA : New York, Seattle
Sign up to request clarification or add additional context in comments.

1 Comment

Nice – now it makes me wonder how to get that irrational spacing in the original 'required' list.
2

Instead of directly printing everything, you could save it to a data structure like a dictionary or collections.defaultdict.

import collections.defaultdict as dd
result = dd(list)
with open('logfile.py', 'rU') as datafile:
    for line in datafile:
        city,country = map(str.strip, line.strip().split(','))
        result[country].append(city)

Then print your results:

for country in result:
    print(country+':', ', '.join(result[country]))

If you think there may be duplicate country/city listings and you don't want them, use set and add instead of list and append.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.