Python Forum
Read a data from text file (Notepad)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read a data from text file (Notepad)
#21
(May-20-2018, 07:24 AM)wavic Wrote: You have to get a data only from the 'capitals' list if the input country is there. If you put: print(countries) print(capitals) in the alif block, you will see that they are in order.
 elif line in countries: print(countries) print(capitals)
If the input country is in the list 'counties' you get the index of the country in that list and print the capital with the corresponding index in the 'capitals' list.

Yeap I understand all of it.
Indeed they are all in orders.

However, It could not pop the exact answer/output.
Example:
If I type "Malaysia", It came out with "The capital of Russia is Moscow" instead of "The capital of Malaysia is Kuala Lumpur". Although they are inside the list.

Output:
No. Countries Capitals 1 Malaysia Kuala Lumpur 2 Japan Tokyo 3 United States of America Washington 4 Canada Ottawa 5 Pakistan Islamabad 6 Australia Canberra 7 Mexico Mexico City 8 Thailand Bangkok 9 Taiwan Taipei 10 China Beijing 11 Czech Republic Prague 12 Russia Moscow Enter a country name (x to Exit): Malaysia The capital of Russia is Moscow Enter a country name (x to Exit): x
Reply
#22
Are you still printing it like this: print ("The capital of {0} is{1}".format(country,capital))?
See my previous post how to do it. Get the index of the input country if it is the same as one of the 'countries' list and print the capital with the same index.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#23
(May-20-2018, 08:51 AM)wavic Wrote: Are you still printing it like this: print ("The capital of {0} is{1}".format(country,capital))? See my previous post how to do it. Get the index of the input country if it is the same as one of the 'countries' list and print the capital with the same index.

Yes I am still gonna need the print ("The capital of {0} is{1}".format(country,capital))

I want to print out like:
Output:
"The capital of Malaysia is Kuala Lumpur"
only

This is the previous result:
Output:
No. Countries Capitals 1 Malaysia Kuala Lumpur 2 Japan Tokyo 3 United States of America Washington 4 Canada Ottawa 5 Pakistan Islamabad 6 Australia Canberra 7 Mexico Mexico City 8 Thailand Bangkok 9 Taiwan Taipei 10 China Beijing 11 Czech Republic Prague 12 Russia Moscow Enter a country name (x to Exit): Malaysia ['Malaysia', 'Japan', 'United States of America', 'Canada', 'Pakistan', 'Australia', 'Mexico', 'Thailand', 'Taiwan', 'China', 'Czech Republic', 'Russia'] [' Kuala Lumpur', ' Tokyo', ' Washington', ' Ottawa', ' Islamabad', ' Canberra', ' Mexico City', ' Bangkok', ' Taipei', ' Beijing', ' Prague', ' Moscow'] The capital of Russia is Moscow
Reply
#24
print("No.\tCountries\t\t\t   Capitals\n ")
f = open(r'C:\Users\USER\Desktop\Notepad_Read\countries.txt')
count = 0
 
data = f.readlines()

for line in data:
    
    line = line.rstrip('\n')
    rec = line.split(',')
    country = rec[0]
    capital = rec[1]
    count = count + 1
    print("{0:1}\t{1:26}\t {2:24}".format(count,country,capital))


countries = [] #Empty list
capitals = []  #Empty list
n = len(countries)


ctry = str(input("Enter a country name (x to Exit): "))
i = 0 

for line in data:
    
    line = line.rstrip('\n')
    rec = line.split(',')
    country = rec[0]
    capital = rec[1]    
    
    countries.append(rec[0]) #Country are stored in countries list
    capitals.append(rec[1])  #Capital are stored in capitals list
    count = count + 1   

    line = line.rstrip('\n')
    country, capital = line.split(',')
    if ctry == 'x':
        break
    elif ctry == countries[i]:  
        print ("The capital of {0} is{1}".format(country,capital))
        break
    else:
        i = i + 1
else:
    print("{0} is not in the list. Please try again.".format (ctry))
    
    
f.close()
I had found the solution.

This is what I needed.
Just share to let everyone make references. x)


CASE CLOSED
Reply
#25
Good!
But if the user keeps with a nonexistent country more than 12 times the script exits with no result at all. Because of the for loop. It loops 12 times only. Also, both lists ( 'countries' and 'capitals' ) are created but never used. However, the script works.

Slightly different approach:
print("No.\tCountries\t\t\t   Capitals\n ")
f = open(r'C:\Users\USER\Desktop\Notepad_Read\countries.txt')
count = 0
  
data = f.readlines()
countries = [] #Empty list
capitals = []  #Empty list
 
for line in data:
     
    line = line.rstrip('\n')
    ciuntry, capital = line.split(',')
    countries.append(country) #Country are stored in countries list
    capitals.append(capital)  #Capital are stored in capitals list
    count = count + 1
    print("{0:2}\t{1:26}\t {2:24}".format(count,country,capital))
 
while True: # it loops until 'x' or the right county
    ctry = input("Enter a country name (x to Exit): ")

    if ctry == 'x' or ctry == 'X':
        break
    elif ctry.title() in countries:
        i = countries.index(ctry.title()) # get the corresponding index
        print ("The capital of {0} is{1}".format(ctry.title(),capitals[i]))
        break
    elif coutry.title() not in countries:
        print("{0} is not in the list. Please try again.".format (ctry))
        continue

f.close()
The title() method is used to guarantee that if the user inputs 'japan' ( all lower case ) the elif conditions will still work.
>>> 'japan'.title()
'Japan'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation URGENT: How to plot data from text file. Trying to recreate plots from MATLAB JamieAl 4 3,473 Dec-03-2023, 06:56 AM
Last Post: Pedroski55
  computer science coursework, read the text please and tell me if theres any specifics sixcray 4 2,558 Nov-11-2020, 03:17 PM
Last Post: buran
  Can we store value in file if we open file in read mode? prasanthbab1234 3 2,509 Sep-26-2020, 12:10 PM
Last Post: ibreeden
  Working with text data APK 4 2,429 Aug-22-2020, 04:48 AM
Last Post: buran
  [split] how to read a specific row in CSV file ? laxmipython 2 8,800 May-22-2020, 12:19 PM
Last Post: Larz60+
  Read data from a CSV file in S3 bucket and store it in a dictionary in python Rupini 3 6,886 May-15-2020, 04:57 PM
Last Post: snippsat
  Read text file, process data and print specific output Happythankyoumoreplease 3 2,851 Feb-20-2020, 12:19 PM
Last Post: jefsummers
  Convert text from an image to a text file Evil_Patrick 5 4,217 Jul-30-2019, 07:57 PM
Last Post: DeaD_EyE
  read from file mcgrim 16 6,022 May-14-2019, 10:31 AM
Last Post: mcgrim
  reading text file and writing to an output file precedded by line numbers kannan 7 10,247 Dec-11-2018, 02:19 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020