Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 285
user3591836 Avatar asked Jun 16 '14 02:06

user3591836


People also ask

How do you write a string with double quotes in Python?

Method #1 : Using backslash (“\”) This is one way to solve this problem. In this, we just employ a backslash before a double quote and it is escaped.

How do you pass a string with double quotes?

The basic double-quoted string is a series of characters surrounded by double quotes. If you need to use the double quote inside the string, you can use the backslash character.

How do you escape a double quote in a string in Python?

Escape from single quote in a string in Python So we need to escape from this single quote. Solutions: Put the string in between double quotes instead of single quotes. Put the escaping character before the single quote in the string.

Can you use double quotes for char?

single quote is for character ; double quote is for string .


1 Answers

\xe2\x80\x9d

Is a unicode value for "special" double quotes. You could decode from UTF-8 into Unicode to convert this into a "single" Unicode character.

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

If you are using 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):

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
like image 190
14 revs, 12 users 16% Avatar answered Sep 28 '22 06:09

14 revs, 12 users 16%