下面为大家分享一篇使用Python和xlwt向Excel文件中写入中文的实例,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧
Python等工具确实是不错的工具,但是有时候不管是基础的Python还是Python的软件包都让我觉得对中文不是很亲近。时不时地遇到一点问题很正常,刚刚在写Excel文件的时候就又遇到了这样的问题。
为了能够说明情况,假设我想把当前文件夹中所有的文件名称全都写入到Excel文件中。
当前的目录信息如下:
grey@DESKTOP-3T80NPQ:/mnt/e/01_workspace/01_docs/02_blog/2017年/08月$ ls -l total 1464 -rwxrwxrwx 1 rootroot 13067 Aug 20 18:58 as86汇编程序的编译与链接.docx -rwxrwxrwx 1 rootroot 13794 Aug 8 22:07 C语言中access函数的使用.docx -rwxrwxrwx 1 rootroot 13285 Aug 8 23:08 C语言中合并字符串.docx -rwxrwxrwx 1 rootroot 15514 Aug 8 23:28 C语言中的动态内存分配.docx -rwxrwxrwx 1 rootroot 13765 Aug 8 23:43 GNU glibc中对C语言标准库中动态内存管理的改进.docx -rwxrwxrwx 1 rootroot 14450 Aug 8 22:37 GNU glibc库中函数执行成功与失败的返回值.docx -rwxrwxrwx 1 rootroot 13485 Aug 20 14:51 Linux 0.12的任务调度猜测.docx -rwxrwxrwx 1 rootroot 11934 Aug 20 13:20 Linux BIOS启动.docx -rwxrwxrwx 1 rootroot 13349 Aug 20 19:38 Linux中dd命令的使用.docx -rwxrwxrwx 1 rootroot 13802 Aug 20 19:20 Linux中使用dd命令修改as86汇编编译链接后的程序.docx -rwxrwxrwx 1 rootroot 15118 Aug 20 16:47 Linux启动代码boot.s理解.docx -rwxrwxrwx 1 rootroot 16005 Aug 20 14:25 Linux操作系统的BIOS启动流程.docx -rwxrwxrwx 1 rootroot 14755 Aug 20 01:29 Linux的proc文件系统.docx -rwxrwxrwx 1 rootroot 12777 Aug 1 23:50 mot文件的单行校验与全局校验.docx -rwxrwxrwx 1 rootroot 131404 Aug 13 14:50 Office 2016 Excel实现下拉栏.docx -rwxrwxrwx 1 rootroot 86665 Aug 19 11:44 Office 2016中修改正文默认字体.docx -rwxrwxrwx 1 rootroot 113476 Aug 9 21:07 PPT中修改已插入对象的图标.docx -rwxrwxrwx 1 rootroot 13636 Aug 16 22:23 Python2与Python3中__bool__方法的差异.docx -rwxrwxrwx 1 rootroot 13924 Aug 1 23:32 Python2与Python3中除法功能的异同.docx -rwxrwxrwx 1 rootroot 14551 Aug 16 23:09 Python2与Python3在不同类的对象比较上的差异.docx -rwxrwxrwx 1 rootroot 14101 Aug 12 10:30 Python abc模块的几个小知识点.docx -rwxrwxrwx 1 rootroot 13581 Aug 9 23:24 Python OOP容器设计时合理引入列表方法.docx -rwxrwxrwx 1 rootroot 12890 Aug 12 14:29 Python中isinstance用法.docx -rwxrwxrwx 1 rootroot 13899 Aug 9 22:46 Python中list的extend方法.docx -rwxrwxrwx 1 rootroot 13142 Aug 18 02:22 Python中__new__方法的使用.docx -rwxrwxrwx 1 rootroot 13280 Aug 20 23:17 Python中使用property实现类的特性.docx -rwxrwxrwx 1 rootroot 13257 Aug 16 22:39 Python中定制类的比较运算方法.docx -rwxrwxrwx 1 rootroot 13897 Aug 18 22:45 Python中最基础类的属性和信息.docx -rwxrwxrwx 1 rootroot 13567 Aug 19 11:53 Python中的@classmethod用法.docx -rwxrwxrwx 1 rootroot 14859 Aug 12 15:32 Python中的join函数用法.docx -rwxrwxrwx 1 rootroot 13832 Aug 18 01:38 Python中的weakref简单小结.docx -rwxrwxrwx 1 rootroot 13314 Aug 16 20:51 Python中的哈希常识小结.docx -rwxrwxrwx 1 rootroot 13822 Aug 12 19:15 Python中的静态函数用法.docx -rwxrwxrwx 1 rootroot 15223 Aug 11 00:12 Python中@符号的用法-1.docx -rwxrwxrwx 1 rootroot 162 Aug 21 23:20 ~$Python和xlwt向Excel文件中写入中文.docx -rwxrwxrwx 1 rootroot 13519 Aug 10 20:01 Python实现接受任意个数参数的函数.docx -rwxrwxrwx 1 rootroot 15114 Aug 19 20:31 Python实现更相减损术求解最大公约数.docx -rwxrwxrwx 1 rootroot 13425 Aug 10 22:51 Python编程中NotImplementedError的使用.docx -rwxrwxrwx 1 rootroot 13345 Aug 20 22:27 Python通过属性手段实现只允许调用一次的方法.docx -rwxrwxrwx 1 rootroot 13586 Aug 19 11:15 Python面向对象编程中属性的基本操作.docx -rwxrwxrwx 1 rootroot 139264 Aug 2 23:50 Simulink解析带有系数和偏移量的CAN报文信息.docx -rwxrwxrwx 1 rootroot 13542 Aug 19 17:22 TAOCP中1.1习题1解答与思考.docx -rwxrwxrwx 1 rootroot 13683 Aug 19 16:05 TAOCP中最大公约数算法理解.docx -rwxrwxrwx 1 rootroot 13539 Aug 20 18:41 Ubuntu上安装as86汇编器.docx -rwxrwxrwx 1 rootroot 203890 Aug 12 14:20 Windows10安装bash.docx -rwxrwxrwx 1 rootroot 258 Aug 21 23:19 xlwt_demo.py -rwxrwxrwx 1 rootroot 13353 Aug 18 02:07 使用__del__方法在Python文件处理时保证文件关闭.docx -rwxrwxrwx 1 rootroot 127142 Aug 7 21:07 使用PolySpace进行模型的检测.docx -rwxrwxrwx 1 rootroot 12430 Aug 21 23:22 使用Python和xlwt向Excel文件中写入中文.docx -rwxrwxrwx 1 rootroot 13303 Aug 19 19:06 辗转相除法求最大公约数.docx
其中,xlwt_demo.py是提前编写Python脚本,代码如下:
#!/usr/bin/python # -*- coding-cp936-*- import os import xlwt from xlwt importWorkbook book = Workbook() sheet1 =book.add_sheet('list') row_num = 0 for f inos.listdir('.'): sheet1.write(row_num,0,f) row_num += 1 book.save('demo.xls')
程序运行结果:
grey@DESKTOP-3T80NPQ:/mnt/e/01_workspace/01_docs/02_blog/2017年/08月$ python xlwt_demo.py Traceback (mostrecent call last): File "xlwt_demo.py", line 14, in<module> book.save('demo.xls') File"/usr/local/lib/python2.7/dist-packages/xlwt/Workbook.py", line 710,in save doc.save(filename_or_stream,self.get_biff_data()) File"/usr/local/lib/python2.7/dist-packages/xlwt/Workbook.py", line 674,in get_biff_data shared_str_table = self.__sst_rec() File"/usr/local/lib/python2.7/dist-packages/xlwt/Workbook.py", line 636,in __sst_rec return self.__sst.get_biff_record() File"/usr/local/lib/python2.7/dist-packages/xlwt/BIFFRecords.py", line77, in get_biff_record self._add_to_sst(s) File"/usr/local/lib/python2.7/dist-packages/xlwt/BIFFRecords.py", line92, in _add_to_sst u_str = upack2(s, self.encoding) File"/usr/local/lib/python2.7/dist-packages/xlwt/UnicodeUtils.py", line50, in upack2 us = unicode(s, encoding) UnicodeDecodeError:'ascii' codec can't decode byte 0xe6 in position 4: ordinal not in range(128)
从上面的提示可以看出,这个工具默认支持的是ASCII码。即使是我在代码的开头指定了编码的格式,依然遇到了问题。如果是这样,那么肯定是xlwt有专门的用法。经过查看文档,修改代码如下:
#!/usr/bin/python # -*- coding:cp936 -*- import os import xlwt from xlwt importWorkbook book =Workbook(encoding='utf-8') sheet1 =book.add_sheet('list') row_num = 0 for f inos.listdir('.'): sheet1.write(row_num,0,f) row_num += 1 book.save('demo.xls')
程序执行结果如下:
grey@DESKTOP-3T80NPQ:/mnt/e/01_workspace/01_docs/02_blog/2017年/08月$ python xlwt_demo.py
grey@DESKTOP-3T80NPQ:/mnt/e/01_workspace/01_docs/02_blog/2017年/08月$ ls
as86汇编程序的编译与链接.docx Python中使用property实现类的特性.docx
C语言中access函数的使用.docx Python中定制类的比较运算方法.docx
C语言中合并字符串.docx Python中最基础类的属性和信息.docx
C语言中的动态内存分配.docx Python中的@classmethod用法.docx
demo.xls Python中的join函数用法.docx
GNU glibc中对C语言标准库中动态内存管理的改进.docx Python中的weakref简单小结.docx
GNU glibc库中函数执行成功与失败的返回值.docx Python中的哈希常识小结.docx
Linux 0.12的任务调度猜测.docx Python中的静态函数用法.docx
Linux BIOS启动.docx Python中@符号的用法-1.docx
Linux中dd命令的使用.docx ~$Python和xlwt向Excel文件中写入中文.docx
Linux中使用dd命令修改as86汇编编译链接后的程序.docx Python实现接受任意个数参数的函数.docx
Linux启动代码boot.s理解.docx Python实现更相减损术求解最大公约数.docx
Linux操作系统的BIOS启动流程.docx Python编程中NotImplementedError的使用.docx
Linux的proc文件系统.docx Python通过属性手段实现只允许调用一次的方法.docx
mot文件的单行校验与全局校验.docx Python面向对象编程中属性的基本操作.docx
Office 2016 Excel实现下拉栏.docx Simulink解析带有系数和偏移量的CAN报文信息.docx
Office 2016中修改正文默认字体.docx TAOCP中1.1习题1解答与思考.docx
PPT中修改已插入对象的图标.docx TAOCP中最大公约数算法理解.docx
Python2与Python3中__bool__方法的差异.docx Ubuntu上安装as86汇编器.docx
Python2与Python3中除法功能的异同.docx Windows10安装bash.docx
Python2与Python3在不同类的对象比较上的差异.docx xlwt_demo.py
Python abc模块的几个小知识点.docx 使用__del__方法在Python文件处理时保证文件关闭.docx
Python OOP容器设计时合理引入列表方法.docx 使用PolySpace进行模型的检测.docx
Python中isinstance用法.docx 使用Python和xlwt向Excel文件中写入中文.docx
Python中list的extend方法.docx 辗转相除法求最大公约数.docx
Python中__new__方法的使用.docx
Excel文件已经生成,具体的内容如下:
汉字写入成功。
值得注意的是,在Windows下编码cp936是可以写入成功的,但是在Linux中只能够用utf-8。这是个很有意思的现象,目前我还没能够想明白。
相关推荐:
以上是使用Python和xlwt向Excel文件中写入中文的详细内容。更多信息请关注PHP中文网其他相关文章!

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造

数组的同质性对性能的影响是双重的:1)同质性允许编译器优化内存访问,提高性能;2)但限制了类型多样性,可能导致效率低下。总之,选择合适的数据结构至关重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,内存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Linux新版
SublimeText3 Linux最新版

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能