Om *_*ary 3 xml entity beautifulsoup xml-parsing python-3.x
我的 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>
Run Code Online (Sandbox Code Playgroud)
现在我可以使用美丽的汤库在 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+"-->"
Run Code Online (Sandbox Code Playgroud)
但是,我无法更改 param 的文本属性,并收到此错误消息
Param.text ="&"+Param["name"]+";<!--"+Param.text+"-->"
AttributeError: can't set attribute
Run Code Online (Sandbox Code Playgroud)
该text属性是只读的,因此您无法修改它,但您可以修改该string属性。
因此,Tag如果您替换.text,则可以更改文本内容,
Param.text ="&"+Param["name"]+";<!--"+Param.text+"-->"
Run Code Online (Sandbox Code Playgroud)
与.string,
Param.string ="&"+Param["name"]+";<!--"+Param.text+"-->"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1084 次 |
| 最近记录: |