我正在尝试从 xlsx 文件创建一个 kml,但是当 xlsx 包含 utf-8 时它不起作用。
我看到了 simplekml 文档,它被标记为已解决,但我无法让它工作。
我尝试将编码设置为 ascii,也使用了 django 的 smart_str 和 smart_unicode,但直到现在都没有任何效果。
我正在使用 openpyxl 读取文件
def create_kml(input_file,sheet_name,output_file,lat_column = 0,lng_column = 1):
kml = simplekml.Kml()
wb_read = load_workbook(input_file)
sh = wb_read.get_sheet_by_name(sheet_name)
properties = []
for c in sh.rows[0]:
properties.append(c.value.encode('utf-8'))
for p,row in enumerate(sh.rows[1:]):
for k,c in enumerate(row):
if k==0:
coord_tuple = (row[lng_column].value,row[lat_column].value)
pnt = kml.newpoint(name = 'Point %s' % p, coords =[coord_tuple])
#TODO: It's not working with unicode and utf-8
if k != lat_column and k != lng_column:
if type(row[k].value) == unicode or type(row[k].value) == str:
pnt.extendeddata.newdata(properties[k],row[k].value.encode('utf-8'))
kml.save(output_file)
追溯:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:/Users/Fernando Alves/Dropbox/Projetos/utils.py", line 296, in create_kml
kml.save(output_file)
File "C:\Python27\lib\site-packages\simplekml\kml.py", line 285, in save
out = self._genkml(format)
File "C:\Python27\lib\site-packages\simplekml\kml.py", line 198, in _genkml
kml_str = self._feature.__str__()
File "C:\Python27\lib\site-packages\simplekml\featgeom.py", line 418, in __str__
buf.append(feat.__str__())
File "C:\Python27\lib\site-packages\simplekml\featgeom.py", line 414, in __str__
buf.append(super(Feature, self).__str__())
File "C:\Python27\lib\site-packages\simplekml\base.py", line 54, in __str__
buf.append(u("<{0}>{1}</{0}>").format(var, val)) # Enclose the variable's __str__ with its name
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 33: ordinal not in range(128)