Re: [Tutor] csv manipulation
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 every 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, Barrybrave White, Barrytall White, Barry52 Rick Davis confident Rick Davis average Rick Davis 48 Jane Doepretty Jane Doeshort 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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] csv manipulation
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, Barrybrave White, Barrytall White, Barry52 Rick Davis confident Rick Davis average Rick Davis 48 Jane Doepretty Jane Doeshort 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
Re: [Tutor] csv manipulation
On Fri, Oct 31, 2008 at 9:32 AM, Serdar Tumgoren [EMAIL PROTECTED] wrote: 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... Use writerow() not writerows() - you are writing a single row. To close the files you have to keep a reference to the underlying file. See my original example. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] csv manipulation
Aha! That did the trick. Thanks so much! On Fri, Oct 31, 2008 at 10:02 AM, Kent Johnson [EMAIL PROTECTED] wrote: On Fri, Oct 31, 2008 at 9:32 AM, Serdar Tumgoren [EMAIL PROTECTED] wrote: 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... Use writerow() not writerows() - you are writing a single row. To close the files you have to keep a reference to the underlying file. See my original example. Kent -- 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
[Tutor] csv manipulation
hello, i have the follwoing csv file: Berat,Berat,Kuçovë,Skrapar Dibër,Bulqizë,Dibër,Mat Durrës,Durrës,Krujë Elbasan,Elbasan,Gramsh,Librazhd,Peqin Fier,Fier,Lushnjë,Mallakastër Gjirokastër,Gjirokastër,Përmet,Tepelenë Korçë,Devoll,Kolonjë,Korçë,Pogradec Kukës,Has,Kukës,Tropojë Lezhë,Kurbin,Lezhë,Mirditë Shkodër,Malësi e Madhe,Pukë,Shkodër Tirana,Kavajë,Tirana Vlorë,Delvinë,Sarandë,Vlorë where thee first column contains the regions and the subsequent the districts. what is the best way to return a file like: Berat,Kuçovë Berat,Skrapar Dibër,Bulqizë Dibër,Dibër Dibër,Mat etc... and to write it out as a new file. thanks Escape to the sun - http://www.tiscali.co.uk/travel/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] csv manipulation
[EMAIL PROTECTED] wrote: hello, i have the follwoing csv file: Berat,Berat,Kuçovë,Skrapar Dibër,Bulqizë,Dibër,Mat Durrës,Durrës,Krujë Elbasan,Elbasan,Gramsh,Librazhd,Peqin Fier,Fier,Lushnjë,Mallakastër Gjirokastër,Gjirokastër,Përmet,Tepelenë Korçë,Devoll,Kolonjë,Korçë,Pogradec Kukës,Has,Kukës,Tropojë Lezhë,Kurbin,Lezhë,Mirditë Shkodër,Malësi e Madhe,Pukë,Shkodër Tirana,Kavajë,Tirana Vlorë,Delvinë,Sarandë,Vlorë where thee first column contains the regions and the subsequent the districts. what is the best way to return a file like: Berat,Kuçovë Berat,Skrapar Dibër,Bulqizë Dibër,Dibër Dibër,Mat etc... and to write it out as a new file. Python has a 'csv' library, which handles your kind of files. I have never used it however, so I cannot give you more details. Good luck, Albert ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] csv manipulation
[EMAIL PROTECTED] wrote: hello, i have the follwoing csv file: Berat,Berat,Kuçovë,Skrapar Dibër,Bulqizë,Dibër,Mat Durrës,Durrës,Krujë Elbasan,Elbasan,Gramsh,Librazhd,Peqin Fier,Fier,Lushnjë,Mallakastër Gjirokastër,Gjirokastër,Përmet,Tepelenë Korçë,Devoll,Kolonjë,Korçë,Pogradec Kukës,Has,Kukës,Tropojë Lezhë,Kurbin,Lezhë,Mirditë Shkodër,Malësi e Madhe,Pukë,Shkodër Tirana,Kavajë,Tirana Vlorë,Delvinë,Sarandë,Vlorë where thee first column contains the regions and the subsequent the districts. what is the best way to return a file like: Berat,Kuçovë Berat,Skrapar Dibër,Bulqizë Dibër,Dibër Dibër,Mat etc... and to write it out as a new file. There is a csv module, but for something this simple the following will suffice: input = open(path-to-the-input-file, 'r') output = open(path-to-the-output-file, 'w') for line in input: line = line.split(,) # [Berat, Berat, Kuçovë, Skrapar] region = line [0] for district in line[1:]: output.write(region + , + district + \n) input.close() output.close() -- Bob Gailer Chapel Hill NC 919-636-4239 When we take the time to be aware of our feelings and needs we have more satisfying interatctions with others. Nonviolent Communication provides tools for this awareness. As a coach and trainer I can assist you in learning this process. What is YOUR biggest relationship challenge? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] csv manipulation OOPS
I goofed - overrode input. So I changed my filenames [snip] inputFile = open(path-to-the-input-file, 'r') outputFile = open(path-to-the-output-file, 'w') for line in inputFile : line = line.split(,) # [Berat, Berat, Kuçovë, Skrapar] region = line [0] for district in line[1:]: outputFile .write(region + , + district + \n) inputFile .close() outputFile .close() -- Bob Gailer Chapel Hill NC 919-636-4239 When we take the time to be aware of our feelings and needs we have more satisfying interatctions with others. Nonviolent Communication provides tools for this awareness. As a coach and trainer I can assist you in learning this process. What is YOUR biggest relationship challenge? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] csv manipulation
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