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.
split()and losing the commas?collections.defaultdictmanages all of that for you