gpt4 book ai didi

python - python 正则表达式编码有问题吗?

转载 作者:行者123 更新时间:2023-12-01 04:47:47 24 4
gpt4 key购买 nike

我有一个很大的 .txt 文件,由以下部分组成:word1word2id编号如下:

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",我想提取以AQRG开头的ids code> 后跟其 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

然后将它们返回到一个元组中,并带有完整的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')]

有人可以帮助我理解上面的示例发生了什么以及如何修复它以捕获保留一个又一个事件的 word2id 吗? 。提前谢谢大家。

最佳答案

在 Python 2 中,对于 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 字符进行处理。

关于python - python 正则表达式编码有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29069928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com