更改花括号内的字符串(选项以|分隔)

2024-06-28 10:56:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试更改以下字符串中花括号之间的文本:

s="As soon as {female_character:Aurelia|Aurelius} turned around the corner, {female_character:she|he} remembered that it was the wrong way and would eventually end in a cul-de-sac. Spinning around, {female_character:Aurelia|Aurelius} tried to run back out, but the way was already blocked by the vendor. In this dark alley way, nobody would see or care what happened to some poor beggar turned thief. Should {female_character:Aurelia|Aurelius} put up a fight in hopes of lasting long enough to escape or give up now and trust to the mercy of the vendor?"

我的问题是,如何将Python逻辑应用于该字符串中的文本,以便{female]_人物:奥雷莉亚|Aurelius}在文本中应用了以下逻辑:

if (whatever is on the left side of the colon) == True:
    (replace {female_character:Aurelia|Aurelius} with the option on the left side of the |) 
else:
    (replace {female_character:Aurelia|Aurelius} with the option on the right side of the |) 

还有几点需要注意,字符串是从json文件中提取出来的,会有许多类似的文本。另外,一些带有的大括号中有这样的大括号:{strong_性格:大因为他年纪小_人物:虽小就他的年龄而言,他是一个非常敏捷的战士|虽然身材一般,但却是一个熟练的战士}}

我相信大家都知道,我对编码还是新手,正在努力学习Python。所以我为自己的无知提前道歉。你知道吗


Tags: oftheto字符串文本onsidefemale
1条回答
网友
1楼 · 发布于 2024-06-28 10:56:11

您可以使用regular expression来定位变量及其文本替换。正则表达式支持分组,因此可以在不同的组中同时获取TrueFalse,然后根据找到的变量的当前值,用正确的组替换整个匹配项。你知道吗

不过,使用嵌套表达式会变得有点困难。最好的方法是构造正则表达式,使其与嵌套的外部级别不匹配。第一次,内括号表达式将被纯文本替换,然后第二个循环将匹配并更改其余的表达式。你知道吗

所以可能需要不止一个替换循环,但是需要多少个呢?这取决于嵌套大括号的数量。您可以将循环设置为“肯定足够大”的数字,例如10,但这有几个缺点。例如,您需要确保不会意外嵌套超过10次;如果您的句子只有一个大括号级别,并且没有嵌套,那么它仍然会循环9次以上,根本不做任何操作。你知道吗

一种方法是计算嵌套大括号的数量。我认为我的findall正则表达式做得很对,但我可能错了。你知道吗

import re

def replaceVars(vars,text):
    for loop in range(len(re.findall(r'\{[^{}]*(?=\{)', text))+1):
        for var in vars:
            if vars[var]:
                text = re.sub ('\{'+var+r':([^|{}]+)\|([^|{}]+?)\}', r'\1', text)
            else:
                text = re.sub ('\{'+var+r':([^|{}]+)\|([^|{}]+?)\}', r'\2', text)
    return text

s = "As soon as {female_character:Aurelia|Aurelius} turned around the corner, {female_character:she|he} remembered that it was the wrong way and would eventually end in a cul-de-sac. Spinning around, {female_character:Aurelia|Aurelius} tried to run back out, but the way was already blocked by the vendor. In this dark alley way, nobody would see or care what happened to some poor beggar turned thief. Should {female_character:Aurelia|Aurelius} put up a fight in hopes of lasting long enough to escape or give up now and trust to the mercy of the vendor? Puppy {strong_character:is big for his age|{small_character:although small for his age, is a very quick warrior|although average size, {female_character:she|he} is a skilled warrior}}"
variables = {"female_character":True, "strong_character":False, "small_character":False}

t = replaceVars(variables,s)
print (t)

结果

As soon as Aurelia turned around the corner, she remembered that it was the wrong way and would eventually end in a cul-de-sac. Spinning around, Aurelia tried to run back out, but the way was already blocked by the vendor. In this dark alley way, nobody would see or care what happened to some poor beggar turned thief. Should Aurelia put up a fight in hopes of lasting long enough to escape or give up now and trust to the mercy of the vendor? Puppy although average size, she is a skilled warrior

相关问题 更多 >