[Tutor] Python xlwt unicoderror
Susana Iraiis Delgado Rodriguez
susana.delgado_s at utzmg.edu.mx
Wed Oct 27 21:42:26 CEST 2010
I'm starting to use xlwt to create excel files, my excel sheet is designed
to get information from a python module. The data came from a raster file,
the problem is that some kind of sign or character doesn't allow the file to
be written. This is my script:
import sys, os, time, socket, codecs
from xlwt import Workbook
from osgeo import gdal
from osgeo.gdalconst import *
from PIL import Image
gdal.AllRegister()
file_list = []
folders = None
# look in this (root) folder for files with specified extension
for root, folders, files in os.walk( "C:\\" ):
file_list.extend(os.path.join(root,fi) for fi in files if
fi.endswith(".tif") or fi.endswith(".tiff") or fi.endswith(".gtif") or
fi.endswith(".ecw") or fi.endswith(".bil") or fi.endswith(".til") or
fi.endswith(".jpeg") or fi.endswith(".img") or fi.endswith(".jpg"))
wrkbk = Workbook()
#Add named parameter to allow overwriting cells: cell_overwrite_ok=True
wksht = wrkbk.add_sheet('raster')
wksht.row(0).write(0,'ruta')
wksht.row(0).write(1,'nombre')
wksht.row(0).write(2,'x_min')
wksht.row(0).write(3,'x_max')
wksht.row(0).write(4,'y_min')
wksht.row(0).write(5,'y_max')
wksht.row(0).write(6,'ancho_pixel')
wksht.row(0).write(7,'alto_pixel')
wksht.row(0).write(8,'num_filas')
wksht.row(0).write(9,'num_columnas')
wksht.row(0).write(10,'num_bandas')
wksht.row(0).write(11,'proyeccion')
wksht.row(0).write(12,'fecha_modificacion')
wksht.row(0).write(13,'maquina_host')
wksht.row(0).write(14,'usuario')
for row, filepath in enumerate(file_list, start=1):
#Llenar lista de archivos y ruta
unicode(filepath, errors='ignore')
wksht.row(row).write(0, filepath)
#Escribir solo el nombre del archivo
(ruta, filename) = os.path.split(filepath)
wksht.row(row).write(1, filename)
unicode(filename, errors='ignore')
# Obtener coordenadas x/y
dataset = gdal.Open(filepath, GA_ReadOnly)
if dataset is None:
print 'Could not open...' +filename
#iNFORMACIoN GEOREFERENCIA
else:
xOrigin, yOrigin, pixelWidth, pixelHeight, a, b =
dataset.GetGeoTransform()
geotransform = dataset.GetGeoTransform(
#Obtener coordenadas
x_min = geotransform[0]
wksht.row(row).write(2,x_min)
y_max = geotransform[3]
wksht.row(row).write(5,y_max)
pixel_width = geotransform[1]
wksht.row(row).write(6,pixel_width)
pixel_height = geotransform[5]
wksht.row(row).write(7,pixel_height)
#Obtener tamano de la imagen
rows = dataset.RasterYSize
cols = dataset.RasterXSize
bands = dataset.RasterCount
wksht.row(row).write(8,rows)
wksht.row(row).write(9,cols)
wksht.row(row).write(10,bands)
x_max= (pixel_width - x_min) / pixel_width
y_min = (y_max - x_min) / pixel_height
wksht.row(row).write(3,x_max)
wksht.row(row).write(4,y_min)
#Obtener la proyeccion
n = os.path.splitext(filepath)
tif = n[0]+'.tif'
tiff = n[0]+'.tiff'
gtif = n[0]+'.gtif'
ecw = n[0]+'.ecw'
bil = n[0]+'.bil'
til = n[0]+'.til'
img = n[0]+'.img'
jpg = n[0]+'.jpg'
jpeg = n[0]+'.jpeg'
if os.path.lexists(tif) or os.path.lexists(tiff) or os.path.lexists(gtif)
or os.path.lexists(ecw) or os.path.lexists(bil) or os.path.lexists(til) or
os.path.lexists(jpg) or os.path.lexists(jpeg):
wksht.row(row).write(11, dataset.GetProjection())
#print filename
else:
wksht.row(row).write(11, "No tiene proyeccion")
#Obtner el nombre del host del archivo
wksht.row(row).write(13, socket.gethostname())
#Obtener usuario
wksht.row(row).write(14,os.environ.get("USERNAME"))
#Obtener fecha de modificacion
t = time.strftime("%m/%d/%Y %I:%M:%S
%p",time.localtime(os.path.getmtime(filepath)))
wksht.row(row).write(15, t)
codecs.EncodedFile(wrkbk, "UTF8",errors='ignore')
wrkbk.save('rasters.xls')
SEARCH_PATH = os.getcwd()
TARGET_FILE = os.path.realpath('shp.xls')
print "Buscando en", SEARCH_PATH, "and writing to", TARGET_FILE
print "Encontrando archivos..."
print "Escribiendo archivo de Excel..."
print "Listo."
The error is:
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)]
on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import crawler_img
ERROR 4: `C:\Archivos de programa\WinAce\menuimg.img' not recognised as a
suppor
ted file format.
Could not open...menuimg.img
ERROR 4: `C:\Archivos de programa\WinAce\toolimg.img' not recognised as a
suppor
ted file format.
Could not open...toolimg.img
ERROR 4: `C:\Documents and Settings\sinventario\Configuraci¾n local\Archivos
tem
porales de Internet\Content.IE5\46MPK9B5\visitor[6].jpg' not recognised as a
sup
ported file format.
Could not open...visitor[6].jpg
ERROR 4: `C:\Documents and Settings\sinventario\Configuraci¾n local\Archivos
tem
porales de Internet\Content.IE5\46MPK9B5\visitor[7].jpg' not recognised as a
sup
ported file format.
Could not open...visitor[7].jpg
ERROR 4: `C:\Documents and Settings\sinventario\Configuraci¾n local\Archivos
tem
porales de Internet\Content.IE5\46MPK9B5\visitor[8].jpg' not recognised as a
sup
ported file format.
Could not open...visitor[8].jpg
ERROR 4: `C:\Documents and Settings\sinventario\Configuraci¾n local\Archivos
tem
porales de Internet\Content.IE5\6LF1W0O1\visitor[1].jpg' not recognised as a
sup
ported file format.
Could not open...visitor[1].jpg
ERROR 4: `C:\Documents and Settings\sinventario\Configuraci¾n local\Archivos
tem
porales de Internet\Content.IE5\6LF1W0O1\visitor[2].jpg' not recognised as a
sup
ported file format.
Could not open...visitor[2].jpg
ERROR 4: `C:\Documents and Settings\sinventario\Configuraci¾n local\Archivos
tem
porales de Internet\Content.IE5\MZ2Z7NIK\visitor[1].jpg' not recognised as a
sup
ported file format.
Could not open...visitor[1].jpg
ERROR 4: `C:\Documents and Settings\sinventario\Configuraci¾n local\Archivos
tem
porales de Internet\Content.IE5\MZ2Z7NIK\visitor[2].jpg' not recognised as a
sup
ported file format.
Could not open...visitor[2].jpg
ERROR 4: `C:\Documents and Settings\sinventario\Configuraci¾n local\Archivos
tem
porales de Internet\Content.IE5\PBD68S04\safe_imageCAO4JFN0.jpg' does not
exist
in the file system,
and is not recognised as a supported dataset name.
Could not open...safe_imageCAO4JFN0.jpg
ERROR 4: `C:\Documents and Settings\sinventario\Configuraci¾n local\Archivos
tem
porales de Internet\Content.IE5\UQM8VB8W\visitor[1].jpg' not recognised as a
sup
ported file format.
Could not open...visitor[1].jpg
ERROR 4: `C:\Documents and Settings\sinventario\Configuraci¾n local\Archivos
tem
porales de Internet\Content.IE5\UQM8VB8W\visitor[2].jpg' not recognised as a
sup
ported file format.
Could not open...visitor[2].jpg
ERROR 4: `C:\Documents and Settings\sinventario\Configuraci¾n local\Archivos
tem
porales de Internet\Content.IE5\X6Q03YQD\visitor[1].jpg' not recognised as a
sup
ported file format.
Could not open...visitor[1].jpg
ERROR 4: `C:\Documents and
Settings\sinventario\Escritorio\Kosmo-2.0\bin\ext\sex
tante\help\es\es.unex.sextante.morphometry.surfaceSpecificPoints\clasificacion_f
ormas_terreno.jpg' not recognised as a supported file format.
Could not open...clasificacion_formas_terreno.jpg
ERROR 4: `C:\Documents and
Settings\sinventario\workspace\libs\sextante\help\es\
es.unex.sextante.morphometry.surfaceSpecificPoints\clasificacion_formas_terreno.
jpg' not recognised as a supported file format.
Could not open...clasificacion_formas_terreno.jpg
ERROR 4: `C:\Kosmo_Desktop_2.0_RC1_src\libs\sextante\help\es\
es.unex.sextante.mo
rphometry.surfaceSpecificPoints\clasificacion_formas_terreno.jpg' not
recognised
as a supported file format.
Could not open...clasificacion_formas_terreno.jpg
ERROR 4: `C:\WINDOWS\ServicePackFiles\i386\netwlan5.img' not recognised as a
sup
ported file format.
Could not open...netwlan5.img
ERROR 4: `C:\WINDOWS\system32\drivers\netwlan5.img' not recognised as a
supporte
d file format.
Could not open...netwlan5.img
ERROR 4:
`C:\workspace\libs\sextante\help\es\es.unex.sextante.morphometry.surfac
eSpecificPoints\clasificacion_formas_terreno.jpg' not recognised as a
supported
file format.
Could not open...clasificacion_formas_terreno.jpg
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "crawler_img.py", line 118, in <module>
wrkbk.save('rasters.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 0xed in position 79:
ordinal
not in range(128)
>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101027/06813af4/attachment-0001.html>
More information about the Tutor
mailing list