小编LCG*_*CGA的帖子

如何从lxml树中删除命名空间?

继续使用python删除XML中的子元素 ...

感谢@Tichodroma,我有这个代码:

如果你可以使用lxml,试试这个:

 import lxml.etree

 tree = lxml.etree.parse("leg.xml")
 for dog in tree.xpath("//Leg1:Dog",
                       namespaces={"Leg1": "http://what.not"}):
     parent = dog.xpath("..")[0]
     parent.remove(dog)
     parent.text = None
 tree.write("leg.out.xml")
Run Code Online (Sandbox Code Playgroud)

现在leg.out.xml看起来像这样:

 <?xml version="1.0"?>
 <Leg1:MOR xmlns:Leg1="http://what.not" oCount="7">
   <Leg1:Order>
     <Leg1:CTemp id="FO">
       <Leg1:Group bNum="001" cCount="4"/>
       <Leg1:Group bNum="002" cCount="4"/>
     </Leg1:CTemp>
     <Leg1:CTemp id="GO">
       <Leg1:Group bNum="001" cCount="4"/>
       <Leg1:Group bNum="002" cCount="4"/>
     </Leg1:CTemp>
   </Leg1:Order>
 </Leg1:MOR>
Run Code Online (Sandbox Code Playgroud)

如何修改我的代码以Leg1:从所有元素的标记名称中删除名称空间前缀?

python xml lxml prefix xml-namespaces

6
推荐指数
1
解决办法
4786
查看次数

如果键存在时键入字典返回false

Python 3.2

这可能真的很愚蠢但是:

items = {'1': 'ACK', '2': 'RELQ', '3': 'COM'}
choice = input('Choose an option:\n1.ACK\n2.RELQ\n3.COM\n')
print(choice)
if choice in items:
    print(choice)
    option = items[choice]
else:
    print('Input not recognized')
Run Code Online (Sandbox Code Playgroud)

如果你输入1,它会一直返回

1
Input not recognized
Run Code Online (Sandbox Code Playgroud)

choice in items是假的吗?

这应该很容易,但我不知道它是什么.

更新:

print(type(choice))` returns str

print(len(choice)) returns 2

print(repr(choice)) returns '1\r'
print(choice[0]) returns 1
Run Code Online (Sandbox Code Playgroud)

输入正在接收带有input()的\ r \n换行符

python if-statement semantics

1
推荐指数
1
解决办法
890
查看次数

标签 统计

python ×2

if-statement ×1

lxml ×1

prefix ×1

semantics ×1

xml ×1

xml-namespaces ×1