Python BeautifulSoup XML, AttributeError: can't set attribute, How to set to text attribute?
Python BeautifulSoup XML, AttributeError: can't set attribute, How to set to text attribute?
我的 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>
现在我可以像这样用 Beautiful soup 库读取 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.text ="&"+Param["name"]+";<!--"+Param.text+"-->"
AttributeError: can't set attribute
text
属性是read-only所以不能修改,但是可以修改string
属性。
所以你可以改变 Tag
的文本内容,如果你替换 .text
,
Param.text ="&"+Param["name"]+";<!--"+Param.text+"-->"
与 .string
、
Param.string ="&"+Param["name"]+";<!--"+Param.text+"-->"
我的 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>
现在我可以像这样用 Beautiful soup 库读取 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.text ="&"+Param["name"]+";<!--"+Param.text+"-->"
AttributeError: can't set attribute
text
属性是read-only所以不能修改,但是可以修改string
属性。
所以你可以改变 Tag
的文本内容,如果你替换 .text
,
Param.text ="&"+Param["name"]+";<!--"+Param.text+"-->"
与 .string
、
Param.string ="&"+Param["name"]+";<!--"+Param.text+"-->"