简体   繁体   English

包含双引号字符的python字符串

[英]python string including double quote character

I have input strings that are comprised of characters, including double and single quotes " and ' 我输入了由字符组成的字符串,包括双引号和单引号“和”

B@SS$*JU(PQ
AD&^%$^@!$
%()%@@DDSFD"*")(#
ABD*E@(%J^&@

however, when I open the above input from a text file and just print it, the double quotes " in the third line get printed as \\xe2\\x80\\x9d 但是,当我从文本文件打开上面的输入并打印它时,第三行中的双引号打印为\\ xe2 \\ x80 \\ x9d

I am aiming to do a simple character count: 我的目标是做一个简单的字符计数:

B 2
@ 3
S 2
$ 3
etc.

so I want to be able to output 所以我希望能够输出

" 3

in the above list. 在上面的列表中。 Should I replace the double quotes with something so I can count them and print out the count? 我应该用一些东西替换双引号,以便我可以计算它们并打印掉计数吗?

Thanks a lot. 非常感谢。

\\xe2\\x80\\x9d \\ XE2 \\ X80 \\ x9d

Is a unicode value for "special" double quotes. 是“特殊”双引号的unicode值。 You could decode from UTF-8 into Unicode to convert this into a "single" Unicode character. 您可以从UTF-8解码为Unicode,将其转换为“单个”Unicode字符。

>>> print "\xe2\x80\x9d".decode("utf-8")
”
>>> len("\xe2\x80\x9d".decode("utf-8"))
1

If you are using Python 3: 如果您使用的是Python 3:

>>> print(b"\xe2\x80\x9d".decode('utf8'))
”
>>> len(b"\xe2\x80\x9d".decode("utf-8"))
1

So for your file that you are counting (in Python 2): 因此,对于您正在计算的文件(在Python 2中):

from collections import defaultdict
with open("filename", 'r') as f:
    for text in f:
        decoded = text.decode("utf-8")
        count = defaultdict(int)
        for i in decoded:
            count[i] += 1

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

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