1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        csv 文件讀寫亂碼問題的一個簡單解決方法

        共 1961字,需瀏覽 4分鐘

         ·

        2020-12-18 19:19

        Python與算法社區(qū)
        第?449?篇原創(chuàng),干貨滿滿
        三步加星標(biāo)


        01

        02

        03

        三步加星標(biāo)






        你好,我是 zhenguo

        今天扼要總結(jié)一個處理csv文件亂碼問題,可能你有類似經(jīng)歷,用excel打開一個csv文件,中文全部顯示亂碼。然后,手動用notepad++打開,修改編碼為utf-8并保存后,再用excel打開顯示正常。

        今天使用Python,很少代碼就能將上面過程自動化。首先,導(dǎo)入3個模塊:

        #?coding:?utf-8
        #?@author:?zhenguo
        #?@date:?2020-12-16
        #?@describe:?functions?about?automatic?file?processing

        import?pandas?as?pd??
        import?os?
        import?chardet

        chardet 模塊用于得到文件的編碼格式,pandas 按照這個格式讀取,然后保存為xlsx格式。

        獲取filename文件的編碼格式:

        def?get_encoding(filename):
        ????"""
        ????返回文件編碼格式
        ????"""

        ????with?open(filename,'rb')?as?f:
        ????????return?chardet.detect(f.read())['encoding']

        保存為utf-8編碼xlsx格式文件,支持csv, xls, xlsx 格式的文件亂碼處理。需要注意,如果讀入文件為csv格式,保存時要使用xlsx格式:

        def?to_utf8(filename):
        ????"""
        ????保存為?to_utf-8
        ????"""

        ????encoding?=?get_encoding(filename)
        ????ext?=?os.path.splitext(filename)
        ????if?ext[1]?=='.csv':
        ????????if?'gb'?in?encoding?or?'GB'?in?encoding:
        ????????????df?=?pd.read_csv(filename,engine='python',encoding='GBK')
        ????????else:
        ????????????df?=?pd.read_csv(filename,engine='python',encoding='utf-8')
        ????????df.to_excel(ext[0]+'.xlsx')
        ????elif?ext[1]=='.xls'?or?ext[1]?==?'.xlsx':
        ????????if?'gb'?in?encoding?or?'GB'?in?encoding:
        ????????????df?=?pd.read_excel(filename,encoding='GBK')
        ????????else:
        ????????????df?=?pd.read_excel(filename,encoding='utf-8')
        ????????df.to_excel(filename)
        ????else:
        ????????print('only?support?csv,?xls,?xlsx?format')

        上面函數(shù)實現(xiàn)單個文件轉(zhuǎn)化,下面batch_to_utf8 實現(xiàn)目錄 path 下所有后綴為ext_name文件的批量亂碼轉(zhuǎn)化:

        def?batch_to_utf8(path,ext_name='csv'):
        ????"""
        ????path下,后綴為?ext_name的亂碼文件,批量轉(zhuǎn)化為可讀文件
        ????"""

        ????for?file?in?os.listdir(path):
        ????????if?os.path.splitext(file)[1]=='.'+ext_name:
        ????????????to_utf8(os.path.join(path,file))

        調(diào)用:

        if?__name__?==?'__main__':
        ??batch_to_utf8('.')?#?對當(dāng)前目錄下的所有csv文件保存為xlsx格式,utf-8編碼的文件

        文件讀寫時亂碼問題,經(jīng)常會遇到,相信今天這篇文章里的to_utf8,batch_to_utf8函數(shù)會解決這個問題,你如果后面遇到,不妨直接引用這兩個函數(shù)嘗試下。

        如果這個亂碼解決方案有遺漏,歡迎你留言補(bǔ)充。最近幾個月整理出來的精華資料已打包為2.0,若你有需要,微信我備注:加油包

        不必打賞
        給我點(diǎn)個贊
        就心滿意足了
        瀏覽 61
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            色悠久久综合 | 免费国产麻豆 | 亚洲系列第一页 | 爱看福利视频 | 与60多岁岳做爰 | 先锋av资源 | 快播一区二区 | 伊人久久香 | 免费观看成人毛片A片入口少 | 国产性生活在线观看 |