[Tutor] CSV to Excel

Susana Iraiis Delgado Rodriguez susana.delgado_s at utzmg.edu.mx
Wed Mar 16 18:12:13 CET 2011


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 <mail at 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110316/8ee35587/attachment.html>


More information about the Tutor mailing list