python 正则表达式编码有问题吗?
Problems with python regex encoding?
我有一个很大的 .txt
文件,由以下内容组成:word1
、word2
、id
、number
,如下所示:
s = '''
Vaya ir VMM03S0 0.427083
mañanita mañana RG 0.796611
, , Fc 1
buscando buscar VMG0000 1
una uno DI0FS0 0.951575
lavadora lavadora NCFS000 0.414738
con con SPS00 1
la el DA0FS0 0.972269
que que PR0CN000 0.562517
sorprender sorprender VMN0000 1
a a SPS00 0.996023
una uno DI0FS0 0.951575
persona persona NCFS000 0.98773
muy muy RG 1
especiales especial AQ0CS0 1
para para SPS00 0.999103
nosotros nosotros PP1MP000 1
, , Fc 1
y y CC 0.999962
la lo PP3FSA00 0.0277039
encontramos encontrar VMIP1P0 0.65
. . Fp 1
Pero pero CC 0.999764
vamos ir VMIP1P0 0.655914
a a SPS00 0.996023
lo el DA0NS0 0.457533
que que PR0CN000 0.562517
interesa interesar VMIP3S0 0.994868
LO_QUE_INTERESA_La lo_que_interesa_la NP00000 1
lavadora lavador AQ0FS0 0.585262
tiene tener VMIP3S0 1
una uno DI0FS0 0.951575
clasificación clasificación NCFS000 1
A+ a+ NP00000 1
, , Fc 1
de de SPS00 0.999984
las el DA0FP0 0.970954
que que PR0CN000 0.562517
ahorran ahorrar VMIP3P0 1
energía energía NCFS000 1
, , Fc 1
si si CS 0.99954
no no RN 0.998134
me me PP1CS000 0.89124
equivoco equivocar VMIP1S0 1
. . Fp 1
Lava lavar VMIP3S0 0.397388
hasta hasta SPS00 0.957698
7 7 Z 1
kg kilogramo NCMN000 1
, , Fc 1
no no RN 0.998134
está estar VAIP3S0 0.999201
nada nada RG 0.135196
mal mal RG 0.497537
, , Fc 1
se se P00CN000 0.465639
le le PP3CSD00 1
veía ver VMII3S0 0.62272
un uno DI0MS0 0.987295
gran gran AQ0CS0 1
tambor tambor NCMS000 1
( ( Fpa 1
de de SPS00 0.999984
acero acero NCMS000 0.973481
inoxidable inoxidable AQ0CS0 1
) ) Fpt 1
y y CC 0.999962
un uno DI0MS0 0.987295
consumo consumo NCMS000 0.948927
máximo máximo AQ0MS0 0.986111
de de SPS00 0.999984
49 49 Z 1
litros litro NCMP000 1
Mandos mandos NP00000 1
intuitivos intuitivo AQ0MP0 1
, , Fc 1
todo todo PI0MS000 0.43165
muy muy RG 1
bien bien RG 0.902728
explicado explicar VMP00SM 1
, , Fc 1
nada nada PI0CS000 0.850279
que que PR0CN000 0.562517
ver ver VMN0000 0.997382
con con SPS00 1
hola RG 0.90937838
como VMP00SM 1
estas AQ089FG 0.90839
la el DA0FS0 0.972269
lavadora lavadora NCFS000 0.414738
de de SPS00 0.999984
casa casa NCFS000 0.979058
de de SPS00 0.999984
mis mi DP1CPS 0.995868
padres padre NCMP000 1
Además además NP00000 1
también también RG 1
seca seco AQ0FS0 0.45723
preciadas preciar VMP00PF 1
. . Fp 1'''
例如对于 s
"file" 我想提取以 AQ
和 RG
开头的 ids
然后是 word2
, 但他们必须 一个接一个 对于上面的例子 这个词保持一个接一个的顺序:
muy muy RG 1
especial especial AQ0CS0 1
对于示例,这个词不包含一个接一个的顺序,所以我不想将它们提取到一个元组中:
hola RG 0.90937838
como VMP00SM 1
estas AQ089FG 0.90839
我想创建一个正则表达式,在元组列表中只提取 word2
后跟它的 id
,如下所示:[('word2','id')]
用于所有 .txt 文件和所有言出必行,一一有序。 对于上面的例子,这是唯一有效的输出:
muy muy RG 1
especiales especial AQ0CS0 1
和
también también RG 1
seca seco AQ0FS0 0.45723
然后 return 将它们放在一个完整的 id
元组中,因为它们保留了一个接一个的顺序:
[('muy', 'RG', 'especial', 'AQ0CS0'), ('también', 'RG', 'seco', 'AQ0FS0')]
我尝试了以下方法:
在:
t = re.findall(r'(\w+)\s*(RG)[^\n]*\n[^\n]*?(\w+)\s*(AQ\w*)', s)
print t
但是我的输出是错误的,因为它去掉了重音和一些字符:
输出:
[('muy', 'RG', 'especial', 'AQ0CS0'), ('n', 'RG', 'seco', 'AQ0FS0')]
而不是,哪个是正确的:
[('muy', 'RG', 'especial', 'AQ0CS0'), ('también', 'RG', 'seco', 'AQ0FS0')]
有人可以帮我理解我上面的例子发生了什么,以及如何修复它以捕捉 word2
和 id
保持一个接一个的发生吗?。提前谢谢大家。
好像\w+不识别特殊字符é。
所以如果你的txt被space严格分割,你可以用\S
替换\w
正则表达式将是
t = re.findall(r'(\S+)\s*(RG)[^\n]*\n[^\n]*?(\S+)\s*(AQ\S*)', s)
在Python2中,对于8位字符串(str
),\w
匹配[0-9a-zA-Z_]
。但是,如果您使用 unicode
并使用 re.UNICODE
标志编译您的模式,则 \w
会根据 unicode 数据库匹配单词字符。
Python documentation 7.2.1 regular expression syntax:
When the LOCALE
and UNICODE
flags are not specified, matches any alphanumeric character and the underscore; this is equivalent to the set [a-zA-Z0-9_]
. With LOCALE
, it will match the set [0-9_]
plus whatever characters are defined as alphanumeric for the current locale. If UNICODE
is set, this will match the characters [0-9_]
plus whatever is classified as alphanumeric in the Unicode character properties database.
这样你就可以做到
u = s.decode('UTF-8') # or whatever encoding is in your text file
t = re.findall(r'(\w+)\s*(RG)[^\n]*\n[^\n]*?(\w+)\s*(AQ\w*)', re.UNICODE)
在 Python 3 中,大部分 str
/unicode
混乱都消失了;当您以文本模式打开文件并读取其内容时,您将得到一个 Python 3 str
对象,它将所有内容都作为 Unicode 字符处理。
我有一个很大的 .txt
文件,由以下内容组成:word1
、word2
、id
、number
,如下所示:
s = '''
Vaya ir VMM03S0 0.427083
mañanita mañana RG 0.796611
, , Fc 1
buscando buscar VMG0000 1
una uno DI0FS0 0.951575
lavadora lavadora NCFS000 0.414738
con con SPS00 1
la el DA0FS0 0.972269
que que PR0CN000 0.562517
sorprender sorprender VMN0000 1
a a SPS00 0.996023
una uno DI0FS0 0.951575
persona persona NCFS000 0.98773
muy muy RG 1
especiales especial AQ0CS0 1
para para SPS00 0.999103
nosotros nosotros PP1MP000 1
, , Fc 1
y y CC 0.999962
la lo PP3FSA00 0.0277039
encontramos encontrar VMIP1P0 0.65
. . Fp 1
Pero pero CC 0.999764
vamos ir VMIP1P0 0.655914
a a SPS00 0.996023
lo el DA0NS0 0.457533
que que PR0CN000 0.562517
interesa interesar VMIP3S0 0.994868
LO_QUE_INTERESA_La lo_que_interesa_la NP00000 1
lavadora lavador AQ0FS0 0.585262
tiene tener VMIP3S0 1
una uno DI0FS0 0.951575
clasificación clasificación NCFS000 1
A+ a+ NP00000 1
, , Fc 1
de de SPS00 0.999984
las el DA0FP0 0.970954
que que PR0CN000 0.562517
ahorran ahorrar VMIP3P0 1
energía energía NCFS000 1
, , Fc 1
si si CS 0.99954
no no RN 0.998134
me me PP1CS000 0.89124
equivoco equivocar VMIP1S0 1
. . Fp 1
Lava lavar VMIP3S0 0.397388
hasta hasta SPS00 0.957698
7 7 Z 1
kg kilogramo NCMN000 1
, , Fc 1
no no RN 0.998134
está estar VAIP3S0 0.999201
nada nada RG 0.135196
mal mal RG 0.497537
, , Fc 1
se se P00CN000 0.465639
le le PP3CSD00 1
veía ver VMII3S0 0.62272
un uno DI0MS0 0.987295
gran gran AQ0CS0 1
tambor tambor NCMS000 1
( ( Fpa 1
de de SPS00 0.999984
acero acero NCMS000 0.973481
inoxidable inoxidable AQ0CS0 1
) ) Fpt 1
y y CC 0.999962
un uno DI0MS0 0.987295
consumo consumo NCMS000 0.948927
máximo máximo AQ0MS0 0.986111
de de SPS00 0.999984
49 49 Z 1
litros litro NCMP000 1
Mandos mandos NP00000 1
intuitivos intuitivo AQ0MP0 1
, , Fc 1
todo todo PI0MS000 0.43165
muy muy RG 1
bien bien RG 0.902728
explicado explicar VMP00SM 1
, , Fc 1
nada nada PI0CS000 0.850279
que que PR0CN000 0.562517
ver ver VMN0000 0.997382
con con SPS00 1
hola RG 0.90937838
como VMP00SM 1
estas AQ089FG 0.90839
la el DA0FS0 0.972269
lavadora lavadora NCFS000 0.414738
de de SPS00 0.999984
casa casa NCFS000 0.979058
de de SPS00 0.999984
mis mi DP1CPS 0.995868
padres padre NCMP000 1
Además además NP00000 1
también también RG 1
seca seco AQ0FS0 0.45723
preciadas preciar VMP00PF 1
. . Fp 1'''
例如对于 s
"file" 我想提取以 AQ
和 RG
开头的 ids
然后是 word2
, 但他们必须 一个接一个 对于上面的例子 这个词保持一个接一个的顺序:
muy muy RG 1
especial especial AQ0CS0 1
对于示例,这个词不包含一个接一个的顺序,所以我不想将它们提取到一个元组中:
hola RG 0.90937838
como VMP00SM 1
estas AQ089FG 0.90839
我想创建一个正则表达式,在元组列表中只提取 word2
后跟它的 id
,如下所示:[('word2','id')]
用于所有 .txt 文件和所有言出必行,一一有序。 对于上面的例子,这是唯一有效的输出:
muy muy RG 1
especiales especial AQ0CS0 1
和
también también RG 1
seca seco AQ0FS0 0.45723
然后 return 将它们放在一个完整的 id
元组中,因为它们保留了一个接一个的顺序:
[('muy', 'RG', 'especial', 'AQ0CS0'), ('también', 'RG', 'seco', 'AQ0FS0')]
我尝试了以下方法:
在:
t = re.findall(r'(\w+)\s*(RG)[^\n]*\n[^\n]*?(\w+)\s*(AQ\w*)', s)
print t
但是我的输出是错误的,因为它去掉了重音和一些字符:
输出:
[('muy', 'RG', 'especial', 'AQ0CS0'), ('n', 'RG', 'seco', 'AQ0FS0')]
而不是,哪个是正确的:
[('muy', 'RG', 'especial', 'AQ0CS0'), ('también', 'RG', 'seco', 'AQ0FS0')]
有人可以帮我理解我上面的例子发生了什么,以及如何修复它以捕捉 word2
和 id
保持一个接一个的发生吗?。提前谢谢大家。
好像\w+不识别特殊字符é。
所以如果你的txt被space严格分割,你可以用\S
替换\w正则表达式将是
t = re.findall(r'(\S+)\s*(RG)[^\n]*\n[^\n]*?(\S+)\s*(AQ\S*)', s)
在Python2中,对于8位字符串(str
),\w
匹配[0-9a-zA-Z_]
。但是,如果您使用 unicode
并使用 re.UNICODE
标志编译您的模式,则 \w
会根据 unicode 数据库匹配单词字符。
Python documentation 7.2.1 regular expression syntax:
When the
LOCALE
andUNICODE
flags are not specified, matches any alphanumeric character and the underscore; this is equivalent to the set[a-zA-Z0-9_]
. WithLOCALE
, it will match the set[0-9_]
plus whatever characters are defined as alphanumeric for the current locale. IfUNICODE
is set, this will match the characters[0-9_]
plus whatever is classified as alphanumeric in the Unicode character properties database.
这样你就可以做到
u = s.decode('UTF-8') # or whatever encoding is in your text file
t = re.findall(r'(\w+)\s*(RG)[^\n]*\n[^\n]*?(\w+)\s*(AQ\w*)', re.UNICODE)
在 Python 3 中,大部分 str
/unicode
混乱都消失了;当您以文本模式打开文件并读取其内容时,您将得到一个 Python 3 str
对象,它将所有内容都作为 Unicode 字符处理。