简体   繁体   English

如何使用openpyxl使UTF-8与Excel文件对应

[英]How to have UTF-8 enconding with an Excel file with openpyxl

With Python and Openpyxl, i have this result when i try to read the sheet name : 使用Python和Openpyxl,当我尝试读取工作表名称时,会得到以下结果:

[u'Janvier ', u'F\\xe9vrier' [u'Janvier',u'F \\ xe9vrier'

The code is : 代码是:

self.classeur = openpyxl.load_workbook('/users/utilisateur/Desktop/Historique.xlsx')
print self.classeur.get_sheet_names()

What can I do to have Février ? 我要怎么做才能拥有Février?

In OOXML all strings are unicode. 在OOXML中,所有字符串都是unicode。 How these appear in your command line depends on a lot things but mainly the configuration of your computer. 这些内容在命令行中的显示方式取决于很多事情,但主要取决于计算机的配置。 As the string is unicode you will need to convert it to the local encoding, assuming this can display the non-ascii characters. 由于字符串是unicode,因此您需要将其转换为本地编码,前提是这可以显示非ASCII字符。

Try: 尝试:

print(s.encode("utf8"))

Note this only affects what you see. 请注意,这只会影响您看到的内容。 If you want to process the content or edit the file, just keep things as unicode. 如果要处理内容或编辑文件,只需将其保留为unicode。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM