gpt4 book ai didi

xml - Python BeautifulSoup XML,AttributeError : can't set attribute, 如何设置为文本属性?

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

我的 Xml 代码看起来像这样

<!-- ************************************************************************ -->
<group title="Test procedure FBlock ablock">
<case ident="Init" title="TS01 Activate" name="TC_Start_Application">
<param name="Min" type="float">0.50
</param>
<param name="Max" type="float">5.00
</param>
</case>
</group>

现在我可以使用 BeautifulSoup 库在 python 中读取 'param' 的文本属性,如下所示:
TTgroup = re.compile('Test someword (.*?): .*')

with open(outFile) as fp:
soup = BeautifulSoup(fp, "lxml")

groups=soup.find_all("group")
for group in groups:
FBlk = group["title"]
FBlk=TTgroup.search(FBlk)

cases = group.find_all("case")
for case in cases:
casetitle = case["title"]
method=str(re.sub(r'TS.*? ', '',casetitle))
Fkt=method.split('.') # split at .
Fkt=str(Fkt[0]) # Function ID from case
method=re.sub('[ (){}<># .,-]', '', method)# Remove unwanted characters
method=method.replace('0x', 'x') # Replace 0x to x

Params = case.find_all("param")
for Param in Params:
if Param["name"] =="Min":
Param.text ="&"+Param["name"]+";<!--"+Param.text+"-->"

但是,我无法更改 param 的文本属性,并收到此错误消息
Param.text ="&"+Param["name"]+";<!--"+Param.text+"-->"
AttributeError: can't set attribute

最佳答案

text属性是只读的,所以你不能修改它,但你可以修改 string属性。
所以你可以改变 Tag如果替换 .text 的文本内容,

Param.text ="&"+Param["name"]+";<!--"+Param.text+"-->"

.string ,
Param.string ="&"+Param["name"]+";<!--"+Param.text+"-->" 

关于xml - Python BeautifulSoup XML,AttributeError : can't set attribute, 如何设置为文本属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49412804/

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