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


Messages In This Thread
Read a data from text file (Notepad) - by Leonzxd - May-18-2018, 03:35 PM
RE: Read a data from text file (Notepad) - by wavic - May-18-2018, 03:47 PM
RE: Read a data from text file (Notepad) - by wavic - May-18-2018, 04:24 PM
RE: Read a data from text file (Notepad) - by wavic - May-18-2018, 04:56 PM
RE: Read a data from text file (Notepad) - by wavic - May-19-2018, 08:17 AM
RE: Read a data from text file (Notepad) - by wavic - May-19-2018, 08:30 AM
RE: Read a data from text file (Notepad) - by wavic - May-19-2018, 09:02 AM
RE: Read a data from text file (Notepad) - by wavic - May-20-2018, 05:38 AM
RE: Read a data from text file (Notepad) - by wavic - May-20-2018, 06:27 AM
RE: Read a data from text file (Notepad) - by wavic - May-20-2018, 06:58 AM
RE: Read a data from text file (Notepad) - by wavic - May-20-2018, 07:24 AM
RE: Read a data from text file (Notepad) - by wavic - May-20-2018, 08:51 AM
RE: Read a data from text file (Notepad) - by Leonzxd - May-22-2018, 05:30 PM
RE: Read a data from text file (Notepad) - by wavic - May-23-2018, 12:17 AM

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,791 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,785 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,688 Sep-26-2020, 12:10 PM
Last Post: ibreeden
  Working with text data APK 4 2,617 Aug-22-2020, 04:48 AM
Last Post: buran
  [split] how to read a specific row in CSV file ? laxmipython 2 9,064 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 7,195 May-15-2020, 04:57 PM
Last Post: snippsat
  Read text file, process data and print specific output Happythankyoumoreplease 3 3,022 Feb-20-2020, 12:19 PM
Last Post: jefsummers
  Convert text from an image to a text file Evil_Patrick 5 4,441 Jul-30-2019, 07:57 PM
Last Post: DeaD_EyE
  read from file mcgrim 16 6,457 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,665 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