简体   繁体   English

涉及特殊字符的Python编码问题

[英]Python encoding issue involving special characters

I am running Win7 x64 and I have Python 2.7.5 x64 installed. 我正在运行Win7 x64,并且安装了Python 2.7.5 x64。 I am using Wing IDE 101 4.1. 我正在使用Wing IDE 101 4.1。

For some reason, encoding is messed up. 由于某种原因,编码混乱了。

special_str = "sauté"
print string
# sauté
string
# 'saut\xc3\xa9'

I don't understand why when I try to print it, it comes out weird. 我不明白为什么当我尝试打印它时会觉得很奇怪。 When I write it to a notepad text file, it comes out as right ("sauté"). 当我将其写入记事本文本文件时,它显示为正确(“sauté”)。 Problem with this is that when I use BeautifulSoup on the string, it comes out containing that weird string "saut├⌐" and then when I output it back into a csv file, I end up with a html chunk containing that weird bit. 问题是,当我在字符串上使用BeautifulSoup时,它包含奇怪的字符串“saut├⌐”,然后当我将其输出回csv文件时,最终得到一个包含该奇怪位的html块。 Help! 救命!

You need to declare the encoding of the source file so Python can properly decode your string literals. 您需要声明源文件的编码,以便Python可以正确解码字符串文字。

You can do this with a special comment at the top of the file (first or second line). 您可以在文件顶部(第一行或第二行)加上特殊注释来执行此操作。

# coding:<coding>

where <coding> is the encoding used when saving the file, for example utf-8 . 其中<coding>是保存文件时使用的编码,例如utf-8

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

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