开发者

Can I do a number of string replace in one statement in python

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 r开发者_如何学Ceplace 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.


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.


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)


Starting with Python 3.4 we can now do

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜