Python program unable to print one line having the same pattern like other lines -
The Python program is unable to print a line with the same pattern as other lines
My Input The last column in the file is the name of the continent. Since there are three entries to end with "Europe \ n" pattern, but only first and last are printed. I am using Python 3 in Ubuntu 14.04
#! / Usr / bin / python fh = open ('countries.txt') in line for fh: if line.endswith ('Europe \ n'): Exit print line () < P> Input: India 1267 746 Asia Mexico 762 78 North America France 211 55 Europe Japan 144 120 Asia Germany 96 61 Europe England 94 56 Europe Output:
France 211 55 Europe England 94 56 Europe < Pre> line for fh: print repr (line)
, which gives the line: ('Europe \ n')
which gives:
'India 1267 746 Asia \ n 'False' Mexico 762 78 North America \ 'False' France 211 55 Europe \ n 'True' Japan 144 120 Asia \ n 'False' Germany 96 61 Europe \ n 'True' England 94 56 Europe 'False' The last line no is included in a new line '\ n' , so it is not printed. You should change this exam:
from line.endswith ('Europe \ n') From:
Line.strip () Endswith ('Europe') i.e. "after the white space has been removed, the line ends with 'Europe' ?" gives it:
India 1267 746 Asia \ n 'false' Mexico 762 78 North America 'false' France 211 55 Europe \ n 'True' Japan 144 120 Asia \ N 'False' Germany 96 61 Europe \ n 'True' England 94 56 Europe True and gives the desired result in your code:
France 211 55 Europe Germany 96 61 Europe England 94 56 Europe
Note that you actually have a reference manager with :
('countries.txt') In the form of FH: for line in fh: if Line.strip () Endwith ('Europe'): Print Line
Comments
Post a Comment