How to detect string byte encoding?

前端 未结 2 853
挽巷
挽巷 2020-12-04 23:51

I\'ve got about 1000 filenames read by os.listdir(), some of them are encoded in UTF8 and some are CP1252.

I want to decode all of them to Unicode for f

2条回答
  •  攒了一身酷
    2020-12-05 00:24

    Use chardet library. It is super easy

    import chardet
    
    the_encoding = chardet.detect('your string')['encoding']
    

    and that's it!

提交回复
热议问题