Re: [Tutor] CSV to Excel
On 16/03/2011 17:12, Susana Iraiis Delgado Rodriguez wrote: Thank you for your help! Once I read your comments I tried both corrections in my code, but none of them we're sucessful. Ok, Susana, your problem (here) is the use of the csv module so can I suggest we back away from your wider program and try to help you understand the way in which that works. Here's a really simple example of how to use the module: code import os import csv # # Open a file which will automatically close once # we're finished # with open (temp.csv, wb) as f: writer = csv.writer (f) # # Write to the csv file with explicit (if meaningless) strings # writer.writerow (['A', 'B', 'C']) list_of_stuff = [Name, 2, 3] # # Write something which is already a list (like your filenames) # writer.writerow (list_of_stuff) # # Now look at the contents of the file produced # print open (temp.csv).read () /code Can you run that and make sure that it produces the .csv format which you'd expect. The output should look something like this: A,B,C Name,2,3 Notice that I'm letting the csv module work out where it needs to put quote marks and where not (which is a lot of the reason for its existence). I just pass it a list of strings, numbers, or whatever and let it turn into a line. I hope that this example helps you to understand the way you would normally use the csv module. Feel free to come back to ask more questions or to see how introduce it into your wider codebase. TJG ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] CSV to Excel
Thank you for your help! Once I read your comments I tried both corrections in my code, but none of them we're sucessful. Tim's idea kept the quotes at the moment I open the csv in Excel, quotues appeared at the beggining and the end of the row for Excel. Joel's idea wrote just tha variable name 'filepath' and ''filename'. The corrections I made were: import os, csv, time, socket from osgeo import ogr,gdal,osr from dbf import * gdal.AllRegister() file_list = [] folders = None for root, folders, files in os.walk( C:\\ ): file_list.extend(os.path.join(root,fi) for fi in files if fi.endswith(.shp)) ofile = open('csv1.csv', wb) writer = csv.writer(open('csv2.csv', wb)) ruta = 'Ruta' archivo = 'archivo' prj = '.prj' campos = [ruta,archivo,prj] writer.writerow(campos) for row, filepath in enumerate(file_list, start=1): (ruta, filename) = os.path.split(filepath) n = os.path.splitext(filepath) p = n[0]+'.prj' filepath = ''+filepath+'' filename = ''+filename+'' if os.path.exists(p): prj_text = open(p, 'r').read() prjtext = ''+prj_text+'' aRow= [ filepath, filename, 1, ] writer.writerow(aRow) else: no_prj = 'Sin prj, no se puede determinar la proyeccion' aRow1= [ filepath, filename,0] writer.writerow(aRow1) print El archivo esta listo 2011/3/11 Tim Golden m...@timgolden.me.uk On 11/03/2011 8:59 PM, Susana Iraiis Delgado Rodriguez wrote: Hello list!! I'm trying to write a CSV file to work it with Excel. My python script is working, the issue is: when I import the file from excel the data comes with quotes at the beginnig and ending of the row. I don't want to have these quotes. What is wrong with my code? Essentially, the work is being done twice. The .writerow method expects a list which it will convert into a quoted, comma-separated string. You're giving it a list whose one element is a quoted, comma-separated string. Just pass it a list instead: writer.writerow (['Ruta', 'Archivo', '.prj']) ... writer.writerow ([filepath, filename, 1]) (BTW, my naive knowledge of Spanish suggests that you're confusing the two identical-sounding English words: root - raiz and route - ruta). TJG ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] CSV to Excel
Hello list!! I'm trying to write a CSV file to work it with Excel. My python script is working, the issue is: when I import the file from excel the data comes with quotes at the beginnig and ending of the row. I don't want to have these quotes. What is wrong with my code? import os, csv from osgeo import ogr,gdal,osr from dbf import * gdal.AllRegister() file_list = [] folders = None for root, folders, files in os.walk( C:\\ ): file_list.extend(os.path.join(root,fi) for fi in files if fi.endswith(.shp)) writer = csv.writer(open('csv2.csv', wb)) campos = ['Ruta,Archivo,.prj'] writer.writerow(campos) for row, filepath in enumerate(file_list, start=1): (ruta, filename) = os.path.split(filepath) n = os.path.splitext(filepath) p = n[0]+'.prj' if os.path.exists(p): aRow = [+filepath+,+filename+,1] writer.writerow(aRow) else: aRow1 = [+filepath+,+filename+,0] writer.writerow(aRow1) print El archivo esta listo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] CSV to Excel
On 11/03/2011 8:59 PM, Susana Iraiis Delgado Rodriguez wrote: Hello list!! I'm trying to write a CSV file to work it with Excel. My python script is working, the issue is: when I import the file from excel the data comes with quotes at the beginnig and ending of the row. I don't want to have these quotes. What is wrong with my code? Essentially, the work is being done twice. The .writerow method expects a list which it will convert into a quoted, comma-separated string. You're giving it a list whose one element is a quoted, comma-separated string. Just pass it a list instead: writer.writerow (['Ruta', 'Archivo', '.prj']) ... writer.writerow ([filepath, filename, 1]) (BTW, my naive knowledge of Spanish suggests that you're confusing the two identical-sounding English words: root - raiz and route - ruta). TJG ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] CSV to Excel
On Fri, Mar 11, 2011 at 3:59 PM, Susana Iraiis Delgado Rodriguez susana.delgad...@utzmg.edu.mx wrote: Hello list!! I'm trying to write a CSV file to work it with Excel. My python script is working, the issue is: when I import the file from excel the data comes with quotes at the beginnig and ending of the row. I don't want to have these quotes. What is wrong with my code? import os, csv from osgeo import ogr,gdal,osr from dbf import * gdal.AllRegister() file_list = [] folders = None for root, folders, files in os.walk( C:\\ ): file_list.extend(os.path.join(root,fi) for fi in files if fi.endswith(.shp)) writer = csv.writer(open('csv2.csv', wb)) campos = ['Ruta,Archivo,.prj'] writer.writerow(campos) for row, filepath in enumerate(file_list, start=1): (ruta, filename) = os.path.split(filepath) n = os.path.splitext(filepath) p = n[0]+'.prj' if os.path.exists(p): aRow = [+filepath+,+filename+,1] I think your problem is in the line above. If you remove the extra quotes the problem goes away filepath = 'filepath' filename = 'filename' aRow = [filepath,filename,1] aRow ['filepath', 'filename', 1] writer.writerow(aRow) else: aRow1 = [+filepath+,+filename+,0] writer.writerow(aRow1) print El archivo esta listo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor