parse a csv file into a text file

Asaf Las roegltd at gmail.com
Wed Feb 5 22:59:30 EST 2014


On Thursday, February 6, 2014 2:46:04 AM UTC+2, Tim Chase wrote:
> On 2014-02-05 16:10, Zhen Zhang wrote:
> Asaf recommended using string methods to split the file.  Keep doing
> what you're doing (using the csv module), as it attends to a lot of
> edge-cases that will trip you up otherwise.  I learned this the hard
> way several years into my Python career. :-)

i did not recommend anything :-) 

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('"'), row[5])



More information about the Python-list mailing list