[Tutor] 'ascii' codec can't decode byte

Susana Iraiis Delgado Rodriguez susana.delgado_s at utzmg.edu.mx
Fri Dec 10 21:45:58 CET 2010


Hello members:

I need your help, I'm developing a python script to make an excel file...
I've been working in this for a long time. The module will write some data
from a .shp file. Unfortuanely that information has some characters
unrecorgnized by ascii. I tried to fix this adding an unicode sentence, but
it didn't work, the script is:
import os, time,fnmatch
from xlwt import Workbook
from osgeo import ogr,gdal,osr
from dbf import *
gdal.AllRegister()
file_list = []
folders = None
for root, folders, files in os.walk( "R:\\" ):
    for filename in fnmatch.filter(files, '*.shp'):
        file_list.append(os.path.join(root, filename)
wrkbk = Workbook()
wksht = wrkbk.add_sheet('shp')
wksht.row(0).write(0,'ruta')
wksht.row(0).write(1,'archivo'
wksht.row(0).write(2,'prj')
wksht.row(0).write(3,'fecha_modificacion')
wksht.row(0).write(4,'maquina_host')
wksht.row(0).write(5,'usuario')
for row, filepath in enumerate(file_list, start=1):
    wksht.row(row).write(0, unicode(filepath,errors='ignore'))
    (ruta, filename) = os.path.split(filepath)
    wksht.row(row).write(1, unicode(filename,errors='ignore'))
    n = os.path.splitext(filepath)
    p = n[0]+'.prj'
    if os.path.exists(p):
       prj_text = open(p, 'r').read()
       unicode(prj_text,errors='ignore')
       wksht.row(row).write(2,prj_text)
 else:
       wksht.row(row).write(2, 'Sin prj, no se puede determinar la
proyeccion')

 wksht.row(row).write(4, unicode(socket.gethostname(),errors='ignore'))

  t = time.strftime("%m/%d/%Y %I:%M:%S
%p",time.localtime(os.path.getmtime(filepath)))
  wksht.row(row).write(3, unicode(t,errors='ignore'))
   wksht.row(row).write(5,unicode(os.environ.get("USERNAME"),errors='ignore'))
wrkbk.save('C:\\Python26\\biblio\\biblio_shp.xls')

When I run it, I got the next error:
Warning 1: organizePolygons() received an unexpected geometry.
Either a polygon with interior rings, or a polygon with less than 4 points,
or a non-Polygon geometry.  Return arguments as a collection.
Warning 1: Geometry of polygon of fid 0 cannot be translated to Simple
Geometry.
 All polygons will be contained in a multipolygon.
El archivo R:\Regiones\BosquePrimavera\Redefinici¾n de ptos del ANP del B la
P\D
atos GPS\Meridian\polnuevonm.shp no tiene dbf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "crawler_shp.py", line 138, in <module>
    wrkbk.save('C:\\Python26\\biblio\\biblio_shp.xls')
  File "C:\Python26\lib\site-packages\xlwt\Workbook.py", line 634, in save
    doc.save(filename, self.get_biff_data())
  File "C:\Python26\lib\site-packages\xlwt\Workbook.py", line 609, in
get_biff_d
ata
    shared_str_table   = self.__sst_rec()
  File "C:\Python26\lib\site-packages\xlwt\Workbook.py", line 571, in
__sst_rec
    return self.__sst.get_biff_record()
  File "C:\Python26\lib\site-packages\xlwt\BIFFRecords.py", line 53, in
get_biff
_record
    self._add_to_sst(s)
  File "C:\Python26\lib\site-packages\xlwt\BIFFRecords.py", line 66, in
_add_to_
sst
    u_str = upack2(s, self.encoding)
  File "C:\Python26\lib\site-packages\xlwt\UnicodeUtils.py", line 50, in
upack2
    us = unicode(s, encoding)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf3 in position 9:
ordinal
not in range(128)
>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101210/95942099/attachment.html>


More information about the Tutor mailing list