2

I am trying to few junk characters in my string using the following statement:

desc = string.replace(desc,'“','"')
desc = string.replace(desc,'”','"')
desc = string.replace(desc,'·','.') 

Can I write the above 3 statements in to a single statement or atlease the 1st two statements to a single statement.

I can not use any third-party libraries in my project.

Edit @unutbu:
My String looks like below:

This is '“' my teststring '”'. I want to replace unicode with appropriate HTML not the whole string only with unicode values.

After using the code:

import HTMLParser

text='“ ” ·'
parser=HTMLParser.HTMLParser()
desc=parser.unescape(text)

I am getting only the HTML equivalents , not the string. But I just want to replace the appropriate values keeping everything in Original String.

I expect the out put as below:

This is "my teststring". I want to replace unicode with appropriate HTML not the whole string only with unicode values.

2
  • Does "I can not use any third-party libraries in my project." mean this is homework? If so please flag it as such. Commented Sep 15, 2011 at 12:50
  • @Duncan3, I am using python for developing Plex plugin n this is going to run on embeded system. Thatz Y I can't use third partie library Commented Sep 15, 2011 at 16:02

3 Answers 3

4

HTMLParser is in the standard library:

import HTMLParser

text='“ ” ·'
parser=HTMLParser.HTMLParser()
desc=parser.unescape(text)
print(desc)
# “ ” ·

If you want that in a single statement, you could of course do

desc=HTMLParser.HTMLParser().unescape(text)

but that might not be an advantage if you need to call unescape in more than one place, and in general, chaining calls like this makes it harder to identify where exceptions occur.

Note that HTMLParser.unescape will unescape all HTML entities defined in htmlentitydefs.names2codepoint (plus ').


Edit: HTMLParser.unescape returns different characters than what you posted. To get exactly those characters, you might use xml.sax.saxutils:

text='“ ” ·'
import xml.sax.saxutils as saxutils
print(saxutils.unescape(text,{'“':'"', '”':'"', '·':'.', }))
# " " .

Note that saxutils.unescape also replaces <, > and &. If you wish to replace only “, ”, and &middot, then I'd use aix's answer.

Sign up to request clarification or add additional context in comments.

7 Comments

Nice as this is, it's worth pointing out that it returns different characters to those given in the OP's code.
@unutbu, By using it the complete string get replaced by HTML chars. Edited the question
@Subhen: Try changing text='“ ” ·' to text="This is “ my teststring ”. I want to replace..."
@subhen: Are there single quotes around '“'? If so, you'll end up with '“'.
@Subhen: Or use desc=parser.unescape(desc) to replace desc with its unescaped version, rather than desc=parser.unescape(text)
|
3

The first two you can do together using regular expressions:

desc = re.sub('&[rl]dquo;', '"', desc)

If you foresee having many such patterns, you could put them into a dictionary and apply in a loop:

patterns = {'&[rl]dquo;': '"',
            '·':   '.'}

for pattern, repl in patterns.items():
    desc = re.sub(pattern, repl, desc)

Like your original code, this doesn't scale well for longer desc since it scans the string multiple times. Here's an extensible version that scans the string just once:

import re

subs = {'rdquo':  '"',
        'ldquo':  '"',
        'middot': '.'}

def repl(matchobj):
  return subs.get(matchobj.group(1), matchobj.group(0))

desc = 'sdf sdfs “ sdf sd “ skdfh · sdf &nonsub; jk'
print re.sub('&(.*?);', repl, desc)

Comments

0

Starting with Python 3.4 we can now do

import html
text='“ ” ·'
desc=html.unescape(text)
print(desc)  # “ ” ·

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.