Compare two chinese words to be equal

Viewed 84

I want to compare two chinese words as below (they should be same):


d = "进
出口中心"
s = "进出口中心"

print(d == s)

They seem to be equal, but not returned equal. When I put these code in to IDLE (Python GUI) it looks like:

>>> d = "进�出口中心"
>>> s = "进出口中心"
>>> print(d == s)

There's a special character � in the word.

How can I remove this character or other special character like it, and make these two words equal?

Thanks!

3 Answers

You can remove non-printable characters from the strings by testing the characters with the str.isprintable method before a comparison is made:

def remove_nonprintable(s):
    return ''.join(c for c in s if c.isprintable())

assert remove_nonprintable(d) == remove_nonprintable(s)

Well one approach here would be to strip off the special separator character(s) first, and then do the comparison:

# -*- coding: utf-8 -*-
import re

d = "进
出口中心"
s = "进出口中心"

d = re.sub(r'[
]', '', d)  # add more characters to the class as necessary
print(d)       # 进出口中心
print(d == s)  # True

It's due to Unicode control characters

U+2028 line separator ,HTML:
,LSEP

U+2029 paragraph separator ,HTML:
,PSEP

So replace these unicode separator works


d = "进
出口中心"
s = "进出口中心"

d = d.replace(chr(8232), "")\
     .replace(chr(8233), "")

print(d == s) # True

Related