The following is an example of using Python and xlwt to write Chinese into an Excel file. It has a good reference value and I hope it will be helpful to everyone. Let's come and take a look
Python and other tools are indeed good tools, but sometimes whether it is basic Python or Python software packages, I feel that I am not very close to Chinese. It's normal to encounter problems from time to time. I just encountered such a problem again when writing an Excel file.
To illustrate the situation, suppose I want to write all the file names in the current folder to an Excel file.
The current directory information is as follows:
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
Among them , xlwt_demo.py is a Python script written in advance, the code is as follows:
#!/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')
Program running result:
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)
As you can see from the above prompt, this tool supports ASCII code by default. Even though I specified the encoding format at the beginning of the code, I still encountered problems. If so, then there must be a specialized usage for xlwt. After checking the document, the modified code is as follows:
#!/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')
##The program execution results are as follows:
grey@DESKTOP-3T80NPQ:/mnt/e/01_workspace/01_docs/02_blog/2017/08$ python xlwt_demo.pygrey@DESKTOP-3T80NPQ:/mnt/e/ 01_workspace/01_docs/02_blog/2017/08$ lsCompilation and linking of as86 assembler.docx Using property in Python to implement class characteristics.docxAccess function in C language Use .docx Comparison operation method of customized classes in Python.docxMerge strings in C language.docx Attributes and information of the most basic classes in Python.docxDynamic memory in C language Allocation.docx @classmethod usage in Python.docxdemo.xls Join function usage in Python.docxImprovements in dynamic memory management in the C language standard library in GNU glibc.docx A brief summary of weakref in Python.docxThe return values of successful and failed function execution in the GNU glibc library.docx A summary of common sense about hashing in Python.docxTask scheduling guesses for Linux 0.12. docx Static function usage in Python.docxLinux BIOS startup.docx Usage of @ symbol in Python-1.docxUsage of dd command in Linux.docx ~$Python and xlwt direction Write Chinese into the Excel file.docxUse the dd command in Linux to modify the as86 assembly compiled and linked program.docx Python implements a function that accepts any number of parameters.docxLinux startup code boot .s understanding.docx Python implements the phase change subtraction technique to solve the greatest common divisor.docxBIOS startup process of Linux operating system.docx The use of NotImplementedError in Python programming.docxLinux proc file System.docx Python uses attributes to implement methods that are only allowed to be called once.docxSingle line verification and global verification of mot files.docx Basic operations of attributes in Python object-oriented programming.docxOffice 2016 Excel implements drop-down bar.docx Simulink parses CAN message information with coefficients and offsets.docxModify the default font of the text in Office 2016.docx Answers and thoughts on 1.1 Exercise 1 in TAOCP.docx Modify the icon of the inserted object in PPT.docx Understanding the greatest common divisor algorithm in TAOCP.docxThe difference between the __bool__ method in Python2 and Python3.docx Install the as86 assembler on Ubuntu. docxSimilarities and differences in division functions between Python2 and Python3.docx Windows10 installation bash.docxDifferences between Python2 and Python3 in comparing objects of different types.docx xlwt_demo.pyA few small knowledge points of the Python abc module.docx Use the __del__ method to ensure that the file is closed during Python file processing.docx Reasonably introduce the list method when designing the Python OOP container.docx Use PolySpace for model detection .docxUsage of isinstance in Python.docx Use Python and xlwt to write Chinese to Excel files.docxThe extend method of list in Python.docx Find the greatest common divisor by euclidean division.docx Usage of __new__ method in Python.docxThe Excel file has been generated, the specific content is as follows:
How to operate excel in a python environment
The above is the detailed content of Write Chinese to Excel file using Python and xlwt. For more information, please follow other related articles on the PHP Chinese website!

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)