May-18-2018, 09:40 AM
Hi all,
I would like to read a data from the text file (Notepad) that contain all sort of countries and its capitals. Such as below:
Malaysia, Kuala Lumpur
Japan, Tokyo
United States of America, Washington
Canada, Ottawa
Pakistan, Islamabad
Australia, Canberra
Mexico, Mexico City
Thailand, Bangkok
Taiwan, Taipei
China, Beijing
Czech Republic, Prague
Russia, Moscow
After that, I want Python to read it. The code would be:
Therefore, I would like to ask is there any code to arrange it all in rows and columns neatly?
Thank you!
I would like to read a data from the text file (Notepad) that contain all sort of countries and its capitals. Such as below:
Malaysia, Kuala Lumpur
Japan, Tokyo
United States of America, Washington
Canada, Ottawa
Pakistan, Islamabad
Australia, Canberra
Mexico, Mexico City
Thailand, Bangkok
Taiwan, Taipei
China, Beijing
Czech Republic, Prague
Russia, Moscow
After that, I want Python to read it. The code would be:
print("No.\tCountries\t\tCapitals") f = open(r'C:\Users\USER\Desktop\Notepad_Read\countries.txt') count = 0 for line in f: line = line.rstrip('\n') rec = line.split(',') count = count + 1 print("{0}\t{1}\t\t{2}".format(count,rec[0],rec[1])) f.closeHowever, the output wasn't organized...
Therefore, I would like to ask is there any code to arrange it all in rows and columns neatly?
Thank you!
