乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 使用<em>python读写excel</em>的问题-python读写excel,python

使用<em>python读写excel</em>的问题-python读写excel,python

作者:乔山办公网日期:

返回目录:excel表格制作


你试这样子。将

jp.write(buf[i].decode('utf-8'))

改成

jp.write(unicode(buf[i]).encode('utf-8'))


python2有自动编码转换的功能。原来type(buf[i])如果是unicode,你还要decode,那么它会自动先按ascii进行encode,变成str型后,再运行decode('utf-8')。所以就会出现你上面的那个错误,汉字是不能encode成ascii的。 


也许刚才的答案是错误的。

你还可以试试

jp.write(buf[i])


另外说明一下。写到socket或者是文件里,必须是string类型。不能是unicode。

这在python2和python3里是一样的。



可以,如果arcgis是10版本,可zd以用arcpy模块中的SearchCursor读取shp的属性表;用python读写excel需要安装pythonWin或者安装comtypes都可以,你可以上网找一下这样的资料


# -*- coding: utf-8 -*-#导入xlwt模块import xlwt# 创建一个Workbook对象,这就相当于创建了一个Excel文件book = xlwt.Workbook(encoding='utf-8', style_compression=0)'''Workbook类初始化时有encoding和style_compression参数encoding:设置字符编码,一般要这样设置:w = Workbook(encoding='utf-8'),就可以在excel中输出中文了636f70797a686964616f334。默认是ascii。当然要记得在文件头部添加:#!/usr/bin/env python# -*- coding: utf-8 -*-style_compression:表示是否压缩,不常用。'''#创建一个sheet对象,一个sheet对象对应Excel文件中的一张表格。# 在电脑桌面右键新建一个Excel文件,其中就包含sheet1,sheet2,sheet3三张表sheet = book.add_sheet('test', cell_overwrite_ok=True)# 其中的test是这张表的名字,cell_overwrite_ok,表示是否可以覆盖单元格,其实是Worksheet实例化的一个参数,默认值是False# 向表test中添加数据sheet.write(0, 0, 'EnglishName') # 其中的'0-行, 0-列'指定表中的单元,'EnglishName'是向该单元写入的内容sheet.write(1, 0, 'Marcovaldo')txt1 = '中文名字'sheet.write(0, 1, txt1.decode('utf-8')) # 此处需要将中文字符串解码成unicode码,否则会报错txt2 = '马可瓦多'sheet.write(1, 1, txt2.decode('utf-8')) # 最后,将以上操作保存到指定的Excel文件中book.save(r'e:\test1.xls') # 在字符串前加r,声明为raw字符串,这样就不会处理其中的转义了。否则,可能会报错

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网