sorry -- forgot to complete my thought in 2nd graf. see below... On Fri, Oct 31, 2008 at 9:29 AM, Serdar Tumgoren <[EMAIL PROTECTED]>wrote:
> Hey everyone, > > I spent a day trying to adapt Mr. Gailer's simple and elegant code to the > csv version suggested by Mr. Johnson, but I can't seem to get it working. > > I adapted the example to my particular use case, but the problem occurs > regardless of the dataset used: Namely, when I loop through the items in the > list of fields and use "writerows" to print to a csv file, the loop splits > the entire line and prints every letter, space, etc. separated by a comma > (rather than the name-attribute pairings I'm aiming for on each line). > > So my starting dataset: > > "White, Barry","brave","tall","52" > "Rick Davis","confident","average","48" > "Jane Doe","pretty","short","40","New York" > "Smith, Janet","organized","65","San Francisco","CA" > "John Quincy","lazy","tall","35" > "Mike Leeds","curious","38" > > ...looks like this: > > W,h,i,t,e,",", ,B,a,r,r,y > b,r,a,v,e > W,h,i,t,e,",", ,B,a,r,r,y > t,a,l,l > W,h,i,t,e,",", ,B,a,r,r,y > 5,2 > R,i,c,k, ,D,a,v,i,s > c,o,n,f,i,d,e,n,t > R,i,c,k, ,D,a,v,i,s > a,v,e,r,a,g,e > R,i,c,k, ,D,a,v,i,s > 4,8 > <snip> > > ...instead of the desired result: > > White, Barry brave > White, Barry tall > White, Barry 52 > Rick Davis confident > Rick Davis average > Rick Davis 48 > Jane Doe pretty > Jane Doe short > <snip> > > When print to the shell, however, I get the results I'm looking for. > > > Below is my code. Can someone tell me how I'm botching the use of the > "writerows" method? Also, on a separate note, is it possible and necessary > to close the input and output files when using csv module? I keep getting a > "module has no close method" error when I try to close the files... > > 1 #!/usr/bin/python > 2 > 3 import csv > 4 > 5 > 6 reader = csv.reader(open('/path/to/infile2.txt', 'rb')) > 7 > 8 > 9 writer = csv.writer(open('/path/to/outfile2.txt', 'wb')) > 10 > 11 #loop through fields in row > 12 for line in reader: > 13 #name is the first field in row > 14 name = line[0] > 15 #create list of person's attributes > 16 attributes = line[1:] > 17 > 18 for characteristic in attributes: > 19 #print to shell for testing > 20 print name + "\t" + characteristic > 21 > 22 #write rows to file > 23 writer.writerows((name,characteristic)) > > > > > > On Wed, Oct 29, 2008 at 1:31 PM, Kent Johnson <[EMAIL PROTECTED]> wrote: > >> On Wed, Oct 29, 2008 at 11:28 AM, bob gailer <[EMAIL PROTECTED]> wrote: >> > [EMAIL PROTECTED] wrote: >> >> >> >> hello, >> >> i have the follwoing csv file: >> >> >> >> "Berat","Berat","Kuçovë","Skrapar" >> >> > There is a csv module, but for something this simple the following will >> > suffice: >> >> as long as none of the data fields include a comma...given that the >> equivalent program using csv is barely longer than your example, and >> more robust, it seems worth using to me. For example (untested): >> >> import csv >> >> inputFile= open(path-to-the-input-file, 'rb') >> reader = csv.reader(inputFile) >> outputFile = open(path-to-the-output-file, 'wb') >> writer = csv.writer(outputFile) >> >> for line in reader: >> region = line [0] >> for district in line[1:]: >> writer.write((region, district)) >> inputFile.close() >> outputFile.close() >> >> Kent >> _______________________________________________ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > -- Serdar Tumgoren The Record 150 River Street Hackensack, NJ 07601 201-403-0834 [EMAIL PROTECTED] northjersey.com
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor