parse a csv file into a text file

Asaf Las roegltd at
Thu Feb 6 09:15:14 CET 2014

On Thursday, February 6, 2014 9:52:43 AM UTC+2, Zhen Zhang wrote:
> On Wednesday, February 5, 2014 7:33:00 PM UTC-5, Roy Smith wrote:
> I failed to figure out why.

OK, you had to look to what i posted second time. The first one is 
irrelevant. Note that file was emulated using StringIO. in your 
case it will be file name. 
You can grab script below and run directly as python script:

<------------------------------------ start of script
import io
import csv

str_t = '''3520005,"Toronto (Ont.)",C  ,F,2503281,2481494,F,F,0.9,1040597,979330,630.1763,3972.4,1
2466023,"Montréal (Que.)",V  ,F,1620693,1583590,T,F,2.3,787060,743204,365.1303,4438.7,2
5915022,"Vancouver (B.C.)",CY ,F,578041,545671,F,F,5.9,273804,253212,114.7133,5039.0,8
3519038,"Richmond Hill (Ont.)",T  ,F,162704,132030,F,F,23.2,53028,51000,100.8917,1612.7,28 '''

file_t = io.StringIO(str_t)

csv_t = csv.reader(file_t, delimiter = ',')
for row in csv_t: 
    print("split result ", row[1].strip('"').split('(')[0] , row[5])

<----------------------------- end of script
Output must be (i got it after run): 

split result  Toronto  2481494
split result  Montréal  1583590
split result  Vancouver  545671
split result  Richmond Hill  132030

row[1].strip('"').split('(')[0]    is City name 
row[5]                             is digits at pos 5 wished

Both are strings, so save them later into file. 
Regarding this one - you can split operations as below to see what is 

Have a nice day 


More information about the Python-list mailing list