
使用Python和xlwt向Excel文件中寫入中文的實(shí)例
Python等工具確實(shí)是不錯(cuò)的工具,但是有時(shí)候不管是基礎(chǔ)的Python還是Python的軟件包都讓我覺得對中文不是很親近。時(shí)不時(shí)地遇到一點(diǎn)問題很正常,剛剛在寫Excel文件的時(shí)候就又遇到了這樣的問題。
為了能夠說明情況,假設(shè)我想把當(dāng)前文件夾中所有的文件名稱全都寫入到Excel文件中。
當(dāng)前的目錄信息如下:
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函數(shù)的使用.docx
-rwxrwxrwx 1 rootroot 13285 Aug 8 23:08 C語言中合并字符串.docx
-rwxrwxrwx 1 rootroot 15514 Aug 8 23:28 C語言中的動態(tài)內(nèi)存分配.docx
-rwxrwxrwx 1 rootroot 13765 Aug 8 23:43 GNU glibc中對C語言標(biāo)準(zhǔn)庫中動態(tài)內(nèi)存管理的改進(jìn).docx
-rwxrwxrwx 1 rootroot 14450 Aug 8 22:37 GNU glibc庫中函數(shù)執(zhí)行成功與失敗的返回值.docx
-rwxrwxrwx 1 rootroot 13485 Aug 20 14:51 Linux 0.12的任務(wù)調(diào)度猜測.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操作系統(tǒng)的BIOS啟動流程.docx
-rwxrwxrwx 1 rootroot 14755 Aug 20 01:29 Linux的proc文件系統(tǒng).docx
-rwxrwxrwx 1 rootroot 12777 Aug 1 23:50 mot文件的單行校驗(yàn)與全局校驗(yàn).docx
-rwxrwxrwx 1 rootroot 131404 Aug 13 14:50 Office 2016 Excel實(shí)現(xiàn)下拉欄.docx
-rwxrwxrwx 1 rootroot 86665 Aug 19 11:44 Office 2016中修改正文默認(rèn)字體.docx
-rwxrwxrwx 1 rootroot 113476 Aug 9 21:07 PPT中修改已插入對象的圖標(biāo).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模塊的幾個(gè)小知識點(diǎn).docx
-rwxrwxrwx 1 rootroot 13581 Aug 9 23:24 Python OOP容器設(shè)計(jì)時(shí)合理引入列表方法.docx
-rwxrwxrwx 1 rootroot 12890 Aug 12 14:29 Python中isinstance用法.docx
-rwxrwxrwx 1 rootroot 13899 Aug 9 22:46 Python中l(wèi)ist的extend方法.docx
-rwxrwxrwx 1 rootroot 13142 Aug 18 02:22 Python中__new__方法的使用.docx
-rwxrwxrwx 1 rootroot 13280 Aug 20 23:17 Python中使用property實(shí)現(xiàn)類的特性.docx
-rwxrwxrwx 1 rootroot 13257 Aug 16 22:39 Python中定制類的比較運(yùn)算方法.docx
-rwxrwxrwx 1 rootroot 13897 Aug 18 22:45 Python中最基礎(chǔ)類的屬性和信息.docx
-rwxrwxrwx 1 rootroot 13567 Aug 19 11:53 Python中的@classmethod用法.docx
-rwxrwxrwx 1 rootroot 14859 Aug 12 15:32 Python中的join函數(shù)用法.docx
-rwxrwxrwx 1 rootroot 13832 Aug 18 01:38 Python中的weakref簡單小結(jié).docx
-rwxrwxrwx 1 rootroot 13314 Aug 16 20:51 Python中的哈希常識小結(jié).docx
-rwxrwxrwx 1 rootroot 13822 Aug 12 19:15 Python中的靜態(tài)函數(shù)用法.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實(shí)現(xiàn)接受任意個(gè)數(shù)參數(shù)的函數(shù).docx
-rwxrwxrwx 1 rootroot 15114 Aug 19 20:31 Python實(shí)現(xiàn)更相減損術(shù)求解最大公約數(shù).docx
-rwxrwxrwx 1 rootroot 13425 Aug 10 22:51 Python編程中NotImplementedError的使用.docx
-rwxrwxrwx 1 rootroot 13345 Aug 20 22:27 Python通過屬性手段實(shí)現(xiàn)只允許調(diào)用一次的方法.docx
-rwxrwxrwx 1 rootroot 13586 Aug 19 11:15 Python面向?qū)ο缶幊讨袑傩缘幕静僮?docx
-rwxrwxrwx 1 rootroot 139264 Aug 2 23:50 Simulink解析帶有系數(shù)和偏移量的CAN報(bào)文信息.docx
-rwxrwxrwx 1 rootroot 13542 Aug 19 17:22 TAOCP中1.1習(xí)題1解答與思考.docx
-rwxrwxrwx 1 rootroot 13683 Aug 19 16:05 TAOCP中最大公約數(shù)算法理解.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文件處理時(shí)保證文件關(guān)閉.docx
-rwxrwxrwx 1 rootroot 127142 Aug 7 21:07 使用PolySpace進(jìn)行模型的檢測.docx
-rwxrwxrwx 1 rootroot 12430 Aug 21 23:22 使用Python和xlwt向Excel文件中寫入中文.docx
-rwxrwxrwx 1 rootroot 13303 Aug 19 19:06 輾轉(zhuǎn)相除法求最大公約數(shù).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')
程序運(yùn)行結(jié)果:
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)
從上面的提示可以看出,這個(gè)工具默認(rèn)支持的是ASCII碼。即使是我在代碼的開頭指定了編碼的格式,依然遇到了問題。如果是這樣,那么肯定是xlwt有專門的用法。經(jīng)過查看文檔,修改代碼如下:
#!/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')
程序執(zhí)行結(jié)果如下:
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實(shí)現(xiàn)類的特性.docx
C語言中access函數(shù)的使用.docx Python中定制類的比較運(yùn)算方法.docx
C語言中合并字符串.docx Python中最基礎(chǔ)類的屬性和信息.docx
C語言中的動態(tài)內(nèi)存分配.docx Python中的@classmethod用法.docx
demo.xls Python中的join函數(shù)用法.docx
GNU glibc中對C語言標(biāo)準(zhǔn)庫中動態(tài)內(nèi)存管理的改進(jìn).docx Python中的weakref簡單小結(jié).docx
GNU glibc庫中函數(shù)執(zhí)行成功與失敗的返回值.docx Python中的哈希常識小結(jié).docx
Linux 0.12的任務(wù)調(diào)度猜測.docx Python中的靜態(tài)函數(shù)用法.docx
Linux BIOS啟動.docx Python中@符號的用法-1.docx
Linux中dd命令的使用.docx ~$Python和xlwt向Excel文件中寫入中文.docx
Linux中使用dd命令修改as86匯編編譯鏈接后的程序.docx Python實(shí)現(xiàn)接受任意個(gè)數(shù)參數(shù)的函數(shù).docx
Linux啟動代碼boot.s理解.docx Python實(shí)現(xiàn)更相減損術(shù)求解最大公約數(shù).docx
Linux操作系統(tǒng)的BIOS啟動流程.docx Python編程中NotImplementedError的使用.docx
Linux的proc文件系統(tǒng).docx Python通過屬性手段實(shí)現(xiàn)只允許調(diào)用一次的方法.docx
mot文件的單行校驗(yàn)與全局校驗(yàn).docx Python面向?qū)ο缶幊讨袑傩缘幕静僮?docx
Office 2016 Excel實(shí)現(xiàn)下拉欄.docx Simulink解析帶有系數(shù)和偏移量的CAN報(bào)文信息.docx
Office 2016中修改正文默認(rèn)字體.docx TAOCP中1.1習(xí)題1解答與思考.docx
PPT中修改已插入對象的圖標(biāo).docx TAOCP中最大公約數(shù)算法理解.docx
Python2與Python3中__bool__方法的差異.docx Ubuntu上安裝as86匯編器.docx
Python2與Python3中除法功能的異同.docx Windows10安裝bash.docx
Python2與Python3在不同類的對象比較上的差異.docx xlwt_demo.py
Python abc模塊的幾個(gè)小知識點(diǎn).docx 使用__del__方法在Python文件處理時(shí)保證文件關(guān)閉.docx
Python OOP容器設(shè)計(jì)時(shí)合理引入列表方法.docx 使用PolySpace進(jìn)行模型的檢測.docx
Python中isinstance用法.docx 使用Python和xlwt向Excel文件中寫入中文.docx
Python中l(wèi)ist的extend方法.docx 輾轉(zhuǎn)相除法求最大公約數(shù).docx
Python中__new__方法的使用.docx
Excel文件已經(jīng)生成,具體的內(nèi)容如下:
漢字寫入成功。
值得注意的是,在Windows下編碼cp936是可以寫入成功的,但是在Linux中只能夠用utf-8。這是個(gè)很有意思的現(xiàn)象,目前我還沒能夠想明白。
以上這篇使用Python和xlwt向Excel文件中寫入中文的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考
數(shù)據(jù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
LSTM 模型輸入長度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報(bào)考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動決策的時(shí)代浪潮下,CDA 數(shù)據(jù)分析師認(rèn)證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計(jì)的實(shí)用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強(qiáng)大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實(shí)施重大更新。 此次更新旨在確保認(rèn) ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價(jià)值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡稱 BI)深度融合的時(shí)代,BI ...
2025-07-10SQL 在預(yù)測分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢預(yù)判? ? 在數(shù)據(jù)驅(qū)動決策的時(shí)代,預(yù)測分析作為挖掘數(shù)據(jù)潛在價(jià)值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價(jià)值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點(diǎn),而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報(bào)考到取證的全攻略? 在數(shù)字經(jīng)濟(jì)蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢性檢驗(yàn):捕捉數(shù)據(jù)背后的時(shí)間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢性檢驗(yàn)如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類型:時(shí)間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時(shí)間是最不可或缺的維度之一,而year_month數(shù)據(jù)類型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實(shí)戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認(rèn)證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗(yàn):數(shù)據(jù)趨勢與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢變化以及識別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認(rèn)證作為國內(nèi)權(quán)威的數(shù)據(jù)分析能力認(rèn)證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對策略? 長短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨(dú)特的門控機(jī)制,在 ...
2025-07-07統(tǒng)計(jì)學(xué)方法在市場調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場調(diào)研是企業(yè)洞察市場動態(tài)、了解消費(fèi)者需求的重要途徑,而統(tǒng)計(jì)學(xué)方法則是市場調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價(jià)值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03