使用Python和xlwt向Excel文件中寫(xiě)入中文的實(shí)例
Python等工具確實(shí)是不錯(cuò)的工具,但是有時(shí)候不管是基礎(chǔ)的Python還是Python的軟件包都讓我覺(jué)得對(duì)中文不是很親近。時(shí)不時(shí)地遇到一點(diǎn)問(wèn)題很正常,剛剛在寫(xiě)Excel文件的時(shí)候就又遇到了這樣的問(wèn)題。
為了能夠說(shuō)明情況,假設(shè)我想把當(dāng)前文件夾中所有的文件名稱全都寫(xiě)入到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語(yǔ)言中access函數(shù)的使用.docx -rwxrwxrwx 1 rootroot 13285 Aug 8 23:08 C語(yǔ)言中合并字符串.docx -rwxrwxrwx 1 rootroot 15514 Aug 8 23:28 C語(yǔ)言中的動(dòng)態(tài)內(nèi)存分配.docx -rwxrwxrwx 1 rootroot 13765 Aug 8 23:43 GNU glibc中對(duì)C語(yǔ)言標(biāo)準(zhǔn)庫(kù)中動(dòng)態(tài)內(nèi)存管理的改進(jìn).docx -rwxrwxrwx 1 rootroot 14450 Aug 8 22:37 GNU glibc庫(kù)中函數(shù)執(zhí)行成功與失敗的返回值.docx -rwxrwxrwx 1 rootroot 13485 Aug 20 14:51 Linux 0.12的任務(wù)調(diào)度猜測(cè).docx -rwxrwxrwx 1 rootroot 11934 Aug 20 13:20 Linux BIOS啟動(dòng).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啟動(dòng)代碼boot.s理解.docx -rwxrwxrwx 1 rootroot 16005 Aug 20 14:25 Linux操作系統(tǒng)的BIOS啟動(dòng)流程.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中修改已插入對(duì)象的圖標(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在不同類的對(duì)象比較上的差異.docx -rwxrwxrwx 1 rootroot 14101 Aug 12 10:30 Python abc模塊的幾個(gè)小知識(shí)點(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ǎn)單小結(jié).docx -rwxrwxrwx 1 rootroot 13314 Aug 16 20:51 Python中的哈希常識(shí)小結(jié).docx -rwxrwxrwx 1 rootroot 13822 Aug 12 19:15 Python中的靜態(tài)函數(shù)用法.docx -rwxrwxrwx 1 rootroot 15223 Aug 11 00:12 Python中@符號(hào)的用法-1.docx -rwxrwxrwx 1 rootroot 162 Aug 21 23:20 ~$Python和xlwt向Excel文件中寫(xiě)入中文.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通過(guò)屬性手段實(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)行模型的檢測(cè).docx -rwxrwxrwx 1 rootroot 12430 Aug 21 23:22 使用Python和xlwt向Excel文件中寫(xiě)入中文.docx -rwxrwxrwx 1 rootroot 13303 Aug 19 19:06 輾轉(zhuǎn)相除法求最大公約數(shù).docx
其中,xlwt_demo.py是提前編寫(xiě)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碼。即使是我在代碼的開(kāi)頭指定了編碼的格式,依然遇到了問(wèn)題。如果是這樣,那么肯定是xlwt有專門(mén)的用法。經(jīng)過(guò)查看文檔,修改代碼如下:
#!/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語(yǔ)言中access函數(shù)的使用.docx Python中定制類的比較運(yùn)算方法.docx
C語(yǔ)言中合并字符串.docx Python中最基礎(chǔ)類的屬性和信息.docx
C語(yǔ)言中的動(dòng)態(tài)內(nèi)存分配.docx Python中的@classmethod用法.docx
demo.xls Python中的join函數(shù)用法.docx
GNU glibc中對(duì)C語(yǔ)言標(biāo)準(zhǔn)庫(kù)中動(dòng)態(tài)內(nèi)存管理的改進(jìn).docx Python中的weakref簡(jiǎn)單小結(jié).docx
GNU glibc庫(kù)中函數(shù)執(zhí)行成功與失敗的返回值.docx Python中的哈希常識(shí)小結(jié).docx
Linux 0.12的任務(wù)調(diào)度猜測(cè).docx Python中的靜態(tài)函數(shù)用法.docx
Linux BIOS啟動(dòng).docx Python中@符號(hào)的用法-1.docx
Linux中dd命令的使用.docx ~$Python和xlwt向Excel文件中寫(xiě)入中文.docx
Linux中使用dd命令修改as86匯編編譯鏈接后的程序.docx Python實(shí)現(xiàn)接受任意個(gè)數(shù)參數(shù)的函數(shù).docx
Linux啟動(dòng)代碼boot.s理解.docx Python實(shí)現(xiàn)更相減損術(shù)求解最大公約數(shù).docx
Linux操作系統(tǒng)的BIOS啟動(dòng)流程.docx Python編程中NotImplementedError的使用.docx
Linux的proc文件系統(tǒng).docx Python通過(guò)屬性手段實(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中修改已插入對(duì)象的圖標(biāo).docx TAOCP中最大公約數(shù)算法理解.docx
Python2與Python3中__bool__方法的差異.docx Ubuntu上安裝as86匯編器.docx
Python2與Python3中除法功能的異同.docx Windows10安裝bash.docx
Python2與Python3在不同類的對(duì)象比較上的差異.docx xlwt_demo.py
Python abc模塊的幾個(gè)小知識(shí)點(diǎn).docx 使用__del__方法在Python文件處理時(shí)保證文件關(guān)閉.docx
Python OOP容器設(shè)計(jì)時(shí)合理引入列表方法.docx 使用PolySpace進(jìn)行模型的檢測(cè).docx
Python中isinstance用法.docx 使用Python和xlwt向Excel文件中寫(xiě)入中文.docx
Python中l(wèi)ist的extend方法.docx 輾轉(zhuǎn)相除法求最大公約數(shù).docx
Python中__new__方法的使用.docx
Excel文件已經(jīng)生成,具體的內(nèi)容如下:
漢字寫(xiě)入成功。
值得注意的是,在Windows下編碼cp936是可以寫(xiě)入成功的,但是在Linux中只能夠用utf-8。這是個(gè)很有意思的現(xiàn)象,目前我還沒(méi)能夠想明白。
以上這篇使用Python和xlwt向Excel文件中寫(xiě)入中文的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- python中使用xlrd、xlwt操作excel表格詳解
- Python使用xlwt模塊操作Excel的方法詳解
- Python xlwt設(shè)置excel單元格字體及格式
- Python第三方庫(kù)xlrd/xlwt的安裝與讀寫(xiě)Excel表格
- python中使用 xlwt 操作excel的常見(jiàn)方法與問(wèn)題
- python使用xlrd與xlwt對(duì)excel的讀寫(xiě)和格式設(shè)定
- python使用xlrd和xlwt讀寫(xiě)Excel文件的實(shí)例代碼
- Python操作excel的方法總結(jié)(xlrd、xlwt、openpyxl)
- Python3使用xlrd、xlwt處理Excel方法數(shù)據(jù)
- 利用Python第三方庫(kù)xlwt寫(xiě)入數(shù)據(jù)到Excel工作表實(shí)例代碼
相關(guān)文章
python中sys.argv參數(shù)用法實(shí)例分析
這篇文章主要介紹了python中sys.argv參數(shù)用法,實(shí)例分析了python中sys.argv參數(shù)的功能、定義及使用技巧,需要的朋友可以參考下2015-05-05用Python的Tornado框架結(jié)合memcached頁(yè)面改善博客性能
這篇文章主要介紹了用Python的Tornado框架結(jié)合memcached頁(yè)面改善vLog性能,主要使用到了緩存來(lái)提升性能,需要的朋友可以參考下2015-04-04python回調(diào)函數(shù)用法實(shí)例分析
這篇文章主要介紹了python回調(diào)函數(shù)用法,較為詳細(xì)的分析了常用的調(diào)用方式,并實(shí)例介紹了Python回調(diào)函數(shù)的使用技巧,需要的朋友可以參考下2015-05-05python之PyAutoGui教你做個(gè)自動(dòng)腳本計(jì)算器的方法
這篇文章主要介紹了python之PyAutoGui教你做個(gè)自動(dòng)腳本計(jì)算器的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03django+tornado實(shí)現(xiàn)實(shí)時(shí)查看遠(yuǎn)程日志的方法
今天小編就為大家分享一篇django+tornado實(shí)現(xiàn)實(shí)時(shí)查看遠(yuǎn)程日志的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08Python 網(wǎng)絡(luò)編程之UDP發(fā)送接收數(shù)據(jù)功能示例【基于socket套接字】
這篇文章主要介紹了Python 網(wǎng)絡(luò)編程之UDP發(fā)送接收數(shù)據(jù)功能,結(jié)合實(shí)例形式分析了Python使用socket套接字實(shí)現(xiàn)基于UDP協(xié)議的數(shù)據(jù)發(fā)送端與接收端相關(guān)操作技巧,需要的朋友可以參考下2019-10-10Python通過(guò)命令開(kāi)啟http.server服務(wù)器的方法
這篇文章主要給大家介紹了關(guān)于Python通過(guò)命令開(kāi)啟http.server服務(wù)器的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11