Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python xml.findall()返回空列表_Python_Xml Parsing_Elementtree - Fatal编程技术网

python xml.findall()返回空列表

python xml.findall()返回空列表,python,xml-parsing,elementtree,Python,Xml Parsing,Elementtree,我尽力解析xml字符串,但在搜索子元素时它返回一个空列表。我可以提供一个工作python小提琴: 但归根结底是: root = ET.fromstring(xml_string) print(root.findall('Race')) 有什么建议吗?看起来像是新手犯的错误 root = ET.fromstring(xml_string) namespace = {'ns': 'http://ergast.com/mrd/1.4' } for race in root.find('ns:Ra

我尽力解析xml字符串,但在搜索子元素时它返回一个空列表。我可以提供一个工作python小提琴:

但归根结底是:

root = ET.fromstring(xml_string)
print(root.findall('Race'))
有什么建议吗?看起来像是新手犯的错误

root = ET.fromstring(xml_string)
namespace = {'ns': 'http://ergast.com/mrd/1.4' }

for race in root.find('ns:RaceTable', namespace):
    round_num = race.get("round")
    circuit = race.find('ns:RaceName', namespace).text
    date = race.find("ns:Date", namespace).text
    time = race.find("ns:Time", namespace).text
    print(round_num, circuit, date, time)
错误:

  • findall()只查找带有标记的元素,这些标记是当前元素的直接子元素
  • 如果XML输入具有前缀形式为prefix:sometag的名称空间、标记和属性,则扩展为{uri}sometag,其中 前缀由完整URI替换。此外,如果存在默认值 命名空间,则该完整URI将被添加到所有非前缀 标签。 签出此文档
  • 您可以使用类似这样的元素。findall()只查找带有标记的元素,这些元素是当前元素的直接子元素。