python - 正则表达式和unicode的问题

时间:2021-03-23 20:13:27

Hi I have a problem in python. I try to explain my problem with an example.

嗨我在python中有问题。我试着通过一个例子来解释我的问题。

I have this string:

我有这个字符串:

>>> string = 'ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿÀÁÂÃ'
>>> print string
ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿÀÁÂÃ

and i want, for example, replace charachters different from Ñ,Ã,ï with ""

我希望,例如,用“”取代不同于Ñ,Ã,ï的字符串

i have tried:

我努力了:

>>> rePat = re.compile('[^ÑÃï]',re.UNICODE)
>>> print rePat.sub("",string)
�Ñ�����������������������������ï�������������������Ã

I obtained this �. I think that it's happen because this type of characters in python are represented by two position in the vector: for example \xc3\x91 = Ñ. For this, when i make the regolar expression, all the \xc3 are not substitued. How I can do this type of sub?????

我得到了这个 。我认为这是因为python中的这种类型的字符由向量中的两个位置表示:例如\ xc3 \ x91 =Ñ。为此,当我进行regolar表达时,所有的\ xc3都不会被替换。我怎么能做这种类型的子?????

Thanks Franco

1 个解决方案

#1


14  

You need to make sure that your strings are unicode strings, not plain strings (plain strings are like byte arrays).

您需要确保您的字符串是unicode字符串,而不是普通字符串(普通字符串就像字节数组)。

Example:

>>> string = 'ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿÀÁÂÃ'
>>> type(string)
<type 'str'>

# do this instead:
# (note the u in front of the ', this marks the character sequence as a unicode literal)
>>> string = u'\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff\xc0\xc1\xc2\xc3'
# or:
>>> string = 'ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿÀÁÂÃ'.decode('utf-8')
# ... but be aware that the latter will only work if the terminal (or source file) has utf-8 encoding
# ... it is a best practice to use the \xNN form in unicode literals, as in the first example

>>> type(string)
<type 'unicode'>
>>> print string
ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿÀÁÂÃ

>>> rePat = re.compile(u'[^\xc3\x91\xc3\x83\xc3\xaf]',re.UNICODE)
>>> print rePat.sub("", string)
Ã

When reading from a file, string = open('filename.txt').read() reads a byte sequence.

从文件中读取时,string = open('filename.txt')。read()读取一个字节序列。

To get the unicode content, do: string = unicode(open('filename.txt').read(), 'encoding'). Or: string = open('filename.txt').read().decode('encoding').

要获取unicode内容,请执行:string = unicode(open('filename.txt')。read(),'encoding')。或者:string = open('filename.txt')。read()。decode('encoding')。

The codecs module can decode unicode streams (such as files) on-the-fly.

编解码器模块可以即时解码unicode流(例如文件)。

Do a google search for python unicode. Python unicode handling can be a bit hard to grasp at first, it pays to read up on it.

做谷歌搜索python unicode。 Python unicode处理起初可能有点难以掌握,阅读它是值得的。

I live by this rule: "Software should only work with Unicode strings internally, converting to a particular encoding on output." (from http://www.amk.ca/python/howto/unicode)

我遵守这条规则:“软件应该只在内部使用Unicode字符串,在输出时转换为特定的编码。” (来自http://www.amk.ca/python/howto/unicode)

I also recommend: http://www.joelonsoftware.com/articles/Unicode.html

我还建议:http://www.joelonsoftware.com/articles/Unicode.html

#1


14  

You need to make sure that your strings are unicode strings, not plain strings (plain strings are like byte arrays).

您需要确保您的字符串是unicode字符串,而不是普通字符串(普通字符串就像字节数组)。

Example:

>>> string = 'ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿÀÁÂÃ'
>>> type(string)
<type 'str'>

# do this instead:
# (note the u in front of the ', this marks the character sequence as a unicode literal)
>>> string = u'\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff\xc0\xc1\xc2\xc3'
# or:
>>> string = 'ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿÀÁÂÃ'.decode('utf-8')
# ... but be aware that the latter will only work if the terminal (or source file) has utf-8 encoding
# ... it is a best practice to use the \xNN form in unicode literals, as in the first example

>>> type(string)
<type 'unicode'>
>>> print string
ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿÀÁÂÃ

>>> rePat = re.compile(u'[^\xc3\x91\xc3\x83\xc3\xaf]',re.UNICODE)
>>> print rePat.sub("", string)
Ã

When reading from a file, string = open('filename.txt').read() reads a byte sequence.

从文件中读取时,string = open('filename.txt')。read()读取一个字节序列。

To get the unicode content, do: string = unicode(open('filename.txt').read(), 'encoding'). Or: string = open('filename.txt').read().decode('encoding').

要获取unicode内容,请执行:string = unicode(open('filename.txt')。read(),'encoding')。或者:string = open('filename.txt')。read()。decode('encoding')。

The codecs module can decode unicode streams (such as files) on-the-fly.

编解码器模块可以即时解码unicode流(例如文件)。

Do a google search for python unicode. Python unicode handling can be a bit hard to grasp at first, it pays to read up on it.

做谷歌搜索python unicode。 Python unicode处理起初可能有点难以掌握,阅读它是值得的。

I live by this rule: "Software should only work with Unicode strings internally, converting to a particular encoding on output." (from http://www.amk.ca/python/howto/unicode)

我遵守这条规则:“软件应该只在内部使用Unicode字符串,在输出时转换为特定的编码。” (来自http://www.amk.ca/python/howto/unicode)

I also recommend: http://www.joelonsoftware.com/articles/Unicode.html

我还建议:http://www.joelonsoftware.com/articles/Unicode.html