Reading text file in Python

Consider there is a file in /tmp/country.txt

contents in the file are like below country name , population , language etc.

United Kingdom,57533000,London,6756000,Europe,1066,1945,-,English
United States,252177000,Washington DC,606900,North America,1776,1945,-,English
Venezuela,19733000,Caracas,1290087,Latin America,1811,1945,-,Spanish
Vietnam,67589000,Hanoi,1088862,Orient,1945,1977,-,Vietnamese
Yemen,1184300,San’a,427150,Asia,1918,1957,Islam,Arabic
Argentina,36955182,Buenos Aires,2033445,Latin America,1853,1945,-,Spanish
Brazil,172860370,Brasilia,286037,0Latin America,1822,1945,-,Portuguese
Bahrain,634137,Manama,34137,Persian Gulf,1973,1977,Islamic,Arabic
Cameroon,15421937,Yaounde,421937,Africa,1960,1974,-,Franch
Djibouti,451442,Djibouti,1442,Africa,1977,1980,-,Franch
Equatorial Guinea,474214,Malabo,74214,Africa,1991,1995,-,Franch
Fiji,832494,Suva,32494,Oceania,1970,1975,-,English
France,59329691,Paris,329691,Europe,486,1945,-,Franch
Greece,10601527,Athens,601527,Europe,1829,1945,-,Greek
Germany,82797408,Berlin,1797408,Europe,1871,1960,-,German
Honduras,6249598,Tegucigalpa,1249598,Latin America,1821,1945,-,Spanish
China,1261832482,Beijing,61832482,Asia,-221,1945,-,Chinese
Canada,31281092,Ottawa,1281092,North America,1867,1945,-,English
Hungary,10138844,Budapest,1138844,Europe,1001,1945,-,Hungerian
India,1014003817,New Delhi,14003817,Asia,1947,1950,-,Indian
Italy,57634327,Rome,3634327,Europe,1861,1950,-,Italian
Ireland,3797257,Dublin,797257,Europe,1921,1945,-,English
Japan,126549976,Tokio,16549976,Asia,-660,1955,-,Japanese
Following program in Python reads all lines one by one & display & counts number of countries having english language

#!/usr/bin/python
print “Reading file /tmp/country.txt\n reading contries with language as English                                                                             ”
fl=open(‘/tmp/country.txt’,’r’)

cnt = 0
for line in fl:
if line.find(‘English’)!= -1:
#               print line;
s1=line.split(‘,’);
print s1[0];
cnt = cnt+1

print “Total Count” , cnt

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.