Python Forum

Full Version: Read a data from text file (Notepad)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Hi once again...

Edit: As previously mentioned, the notepad consists of 12 records of countries such as:

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

Thank you for helping me with the previous post.
However, right now I would like to search a COUNTRY in the text file (Notepad).
This is the best code I can think of.

print("No.\tCountries\t\t\t   Capitals")
f = open(r'C:\Users\USER\Desktop\Notepad_Read\countries.txt')
count = 0

for line in f:
    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))


while True:
    line = str(input("Enter a country name (x to Exit):"))
    if line == 'x':
        break
    elif line == country: #Read the country name
        list = line.split(',')
        print ("The capital of {1} is {2}".format(country,capital))
        
    else:
        list = line.split(',')
        print("The country is not in the list. Please try again.")


f.close()
I want to INPUT a country name and show it out its capital.
But it wouldn't let me read the data I stored.
Please help once again...

Thank you! Doh

I changed here:

    elif line == country:
        list = line.split(',')
        print ("The capital of {0} is {1}".format(country,capital))
However, it only seems to let me read Russia, but other countries I couldn't pop out the answer.

Need Help!
This is because the iterator ( 'f' is exhausted ). During the reading from a file ( the for loop ) store the line in a list or a dictionary or a set ) so you can use it later. Or you can use f.readlines() method which will create a list and iterate over the list instead of 'f'.

data = f.readlines()
for line in data:
    # ...
(May-18-2018, 03:47 PM)wavic Wrote: [ -> ]This is because the iterator ( 'f' is exhausted ). During the reading from a file ( the for loop ) store the line in a list or a dictionary or a set ) so you can use it later. Or you can use f.readlines() method which will create a list and iterate over the list instead of 'f'.
data = f.readlines() for line in data: # ...

Hi wavic!
Thanks for your reply,
As you mentioned the word "Exhausted" does that meant by "Used up the storage" right?
Therefore, would it be possible to use once again?

I used your suggestion the f.readlines() method.

However, it could not execute the "Enter a country name (x to Exit):".
Post the code, please!
(May-18-2018, 04:24 PM)wavic Wrote: [ -> ]Post the code, please!

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))
    

while (True):        
    line = str(input("Enter a country name (x to Exit): "))
    if line == 'x':
        break
    elif line == country:
        
        print ("The capital of {0} is {1}".format(country,capital))
        
    else:
        
        print("The country is not in the list. Please try again.")
        

f.close()
It could read Russia only. Other countries it couldn't be executed...

PS: Somehow I restart the Spyder then it worked.
The 'country' is set to Russia on line 9 during the last loop.
You have to traverse trough data again and to check if the input is equal to the str.startswith from each element.
I would use a dictionary but I don't know what is the exact assignment or whether you had the dictionaries at school.
(May-18-2018, 04:56 PM)wavic Wrote: [ -> ]The 'country' is set to Russia on line 9 during the last loop. You have to traverse trough data again and to check if the input is equal to the str.startswith from each element. I would use a dictionary but I don't know what is the exact assignment or whether you had the dictionaries at school.

Okay now I noticed.
The full question would be:
Create a text file called countries to store a list of n=12 country names with their capitals. Write a program to read the same into lists country and capital respectively. Then, when the user enters a country name, the program displays the message "The capital of {Country} is {Capital}" if there is a match, or the message "The country is not in the lists" if there is no match.

Now I notice, after reading the text file, it wants to program/store into a list. Is this possible?
I Will try at the same time
You have 'country' and 'capital'. Adding each of them to a corresponding list is a routine.
(May-19-2018, 08:17 AM)wavic Wrote: [ -> ]You have 'country' and 'capital'. Adding each of them to a corresponding list is a routine.

Hi wavic!

Sorry I dont understand the "Adding each of them to a corresponding list is a routine."
Which means It cant store the whole list at once? But only store line by line?

(May-18-2018, 04:56 PM)wavic Wrote: [ -> ]The 'country' is set to Russia on line 9 during the last loop. You have to traverse trough data again and to check if the input is equal to the str.startswith from each element. I would use a dictionary but I don't know what is the exact assignment or whether you had the dictionaries at school.

May I know how to traverse the data again?
Is there any code for it?

Sorry for not understanding well.
>>> l = list('some data')
>>> for element in l:
...     print(element)
... 
s
o
m
e
 
d
a
t
a
>>> for element in l:
...     print(element*4)
... 
ssss
oooo
mmmm
eeee
    
dddd
aaaa
tttt
aaaa
Here I loop again through the list to do something else.


As I understand the assignment you have to have one list for the countries and one to store the capitals. But this is the mine interpretation.
Quote:... to read the same into lists country and capital respectively
Pages: 1 2 3