最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python和xlwt向Excel文件中寫(xiě)入中文的實(shí)例

 更新時(shí)間:2018年04月21日 09:09:24   作者:grey_csdn  
下面小編就為大家分享一篇使用Python和xlwt向Excel文件中寫(xiě)入中文的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

Python等工具確實(shí)是不錯(cuò)的工具,但是有時(shí)候不管是基礎(chǔ)的Python還是Python的軟件包都讓我覺(jué)得對(duì)中文不是很親近。時(shí)不時(shí)地遇到一點(diǎn)問(wèn)題很正常,剛剛在寫(xiě)Excel文件的時(shí)候就又遇到了這樣的問(wèn)題。

為了能夠說(shuō)明情況,假設(shè)我想把當(dāng)前文件夾中所有的文件名稱(chē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在不同類(lèi)的對(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)類(lèi)的特性.docx
-rwxrwxrwx 1 rootroot 13257 Aug 16 22:39 Python中定制類(lèi)的比較運(yùn)算方法.docx
-rwxrwxrwx 1 rootroot 13897 Aug 18 22:45 Python中最基礎(chǔ)類(lèi)的屬性和信息.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有專(zhuān)門(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)類(lèi)的特性.docx

C語(yǔ)言中access函數(shù)的使用.docx Python中定制類(lèi)的比較運(yùn)算方法.docx

C語(yǔ)言中合并字符串.docx Python中最基礎(chǔ)類(lèi)的屬性和信息.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在不同類(lèi)的對(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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python爬蟲(chóng)請(qǐng)求模塊Urllib及Requests庫(kù)安裝使用教程

    Python爬蟲(chóng)請(qǐng)求模塊Urllib及Requests庫(kù)安裝使用教程

    requests和urllib都是Python中常用的HTTP請(qǐng)求庫(kù),使用時(shí)需要根據(jù)實(shí)際情況選擇,如果要求使用簡(jiǎn)單、功能完善、性能高的HTTP請(qǐng)求庫(kù),可以選擇requests,如果需要兼容性更好、功能更加靈活的HTTP請(qǐng)求庫(kù),可以選擇urllib
    2023-11-11
  • Python 刪除整個(gè)文本中的空格,并實(shí)現(xiàn)按行顯示

    Python 刪除整個(gè)文本中的空格,并實(shí)現(xiàn)按行顯示

    今天小編就為大家分享一篇Python 刪除整個(gè)文本中的空格,并實(shí)現(xiàn)按行顯示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • Python繪制分段函數(shù)的實(shí)現(xiàn)示例

    Python繪制分段函數(shù)的實(shí)現(xiàn)示例

    本文主要介紹了Python繪制分段函數(shù)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • python文件與路徑管理方法

    python文件與路徑管理方法

    這篇文章主要介紹了python文件與路徑管理方法,文章屬于python使用技巧的分享,下文圍繞文件與路徑管理相關(guān)內(nèi)容,需要的小伙伴可以參考一下,希望對(duì)你有所幫助
    2022-02-02
  • Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)字典相關(guān)計(jì)算問(wèn)題示例

    Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)字典相關(guān)計(jì)算問(wèn)題示例

    這篇文章主要介紹了Python字典相關(guān)計(jì)算問(wèn)題,結(jié)合實(shí)例形式總結(jié)分析了Python字典相關(guān)的最小值、最大值、排序等操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-02-02
  • Numpy創(chuàng)建NumPy矩陣的簡(jiǎn)單實(shí)現(xiàn)

    Numpy創(chuàng)建NumPy矩陣的簡(jiǎn)單實(shí)現(xiàn)

    本文主要介紹了Numpy創(chuàng)建NumPy矩陣的簡(jiǎn)單實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Python設(shè)計(jì)模式結(jié)構(gòu)型享元模式

    Python設(shè)計(jì)模式結(jié)構(gòu)型享元模式

    這篇文章主要介紹了Python享元模式,享元模式即Flyweight Pattern,指運(yùn)用共享技術(shù)有效地支持大量細(xì)粒度的對(duì)象,下面和小編一起進(jìn)入文章了解更多詳細(xì)內(nèi)容吧
    2022-02-02
  • Python多線(xiàn)程編程(五):死鎖的形成

    Python多線(xiàn)程編程(五):死鎖的形成

    這篇文章主要介紹了Python多線(xiàn)程編程(五):死鎖的形成,本文講解了死鎖的概念、死鎖示例、避免死鎖的方法等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Python+Pygame實(shí)現(xiàn)簡(jiǎn)單的射擊小游戲

    Python+Pygame實(shí)現(xiàn)簡(jiǎn)單的射擊小游戲

    要說(shuō)什么游戲能夠獲得大家的喜愛(ài),唯射擊游戲莫屬!本文將利用Python和Pygame庫(kù)制作一個(gè)簡(jiǎn)單的射擊小游戲,感興趣的小伙伴可以了解一下
    2022-04-04
  • Python接口自動(dòng)化淺析pymysql數(shù)據(jù)庫(kù)操作流程

    Python接口自動(dòng)化淺析pymysql數(shù)據(jù)庫(kù)操作流程

    本文主要介紹pymysql安裝、操作流程、語(yǔ)法基礎(chǔ)及封裝操作數(shù)據(jù)庫(kù)類(lèi),需要的朋友可以參考下,希望能對(duì)大家有所幫助,每日提升一點(diǎn)點(diǎn),歡迎大家多多交流討論
    2021-08-08

最新評(píng)論

沐川县| 化德县| 呼图壁县| 镇平县| 黄大仙区| 永靖县| 镶黄旗| 尼勒克县| 孟连| 武定县| 兴文县| 丹阳市| 驻马店市| 盱眙县| 新晃| 仁化县| 新沂市| 康马县| 怀远县| 广河县| 闽侯县| 工布江达县| 平罗县| 衡南县| 牡丹江市| 贡嘎县| 黄浦区| 汕尾市| 安康市| 吉林市| 阳新县| 盐源县| 商水县| 稷山县| 宁阳县| 万全县| 青冈县| 东乡县| 江源县| 博兴县| 东兴市|