python - parse a csv file into a text file -
i second year ee student. started learning python project.
i intend parse csv file format
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
into text file following
toronto 2503281 montreal 1620693 vancouver 578041
i extracting 1st , 5th column , save text file.
this have far.
import csv file = open('raw.csv') reader = csv.reader(file) f = open('nicelydone.text','w') line in reader: f.write("%s %s"%line[1],%line[5])
this not working me, able extract data csv file line[1],line[5]. (i able print out) dont know how write .text file in format wanted.
also, have process first column eg, "toronto (ont.)" "toronto". familiar function find(), assume extract toronto out of toronto(ont.) using "(" stopping character, based on research , have no idea how use , ask return me string(toronto).
here question:
- what data format
line[1]
?- if string how come
f.write()
not work? - if not string, how convert string?
- if string how come
- how extract word
toronto
out oftoronto(ont)
string form usingfind()
or other methods.
my thinking add 2 string c = a+ ' ' + b
, give me format wanted. can use f.write()
write file :)
sorry if questions sounds easy or stupid.
thanks ahead
zhen
- all data read
csv.reader
strings. there variety of solutions this, simplest split on
(
, strip away whitespace:>>> = 'toronto (ont.)' >>> b = a.split('(') >>> b out[16]: ['toronto ', 'ont.)'] >>> c = b[0] >>> c out[18]: 'toronto ' >>> c.strip() out[19]: 'toronto'
or in 1 line:
>>> print 'toronto (ont.)'.split('(')[0].strip()
another option have been use regular expression (the re module).
the specific problem in code lies here:
f.write("%s %s"%line[1],%line[5])
using %
syntax format string, have provide either single value, or iterable. in case should be:
f.write("%s %s" % (line[1], line[5]))
another way exact same thing, use format
method.
f.write('{} {}'.format(line[1], line[5]))
this flexible way of formating strings, , recommend read in docs.
regarding code, there couple of things should consider.
always remember close file handlers. if use
with open(...) fp
, taken care of you.with open('myfile.txt') ifile: # stuff # file closed here
don't use reserved words variable name.
file
such thing, , using else (shadowing it), may cause problems later on in code.to write data, can use csv.writer:
with open('myfile.txt', 'wb') ofile: writer = csv.writer(ofile) writer.writerow(['my', 'data'])
from python 2.6 , above, can combine multiple
with
statements in 1 statement:with open('raw.csv') ifile, open('nicelydone.text','w') ofile: reader = csv.reader(ifile) writer = csv.writer(ofile)
combining knowledge, script can rewritten like:
import csv open('raw.csv') ifile, open('nicelydone.text', 'wb') ofile: reader = csv.reader(ifile) writer = csv.writer(ofile, delimiter=' ') row in reader: city, num = row[1].split('(')[0].strip(), row[5] writer.writerow([city, num])
Comments
Post a Comment