Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.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 脚本运行不正确。无输出_Python_Python 3.x - Fatal编程技术网

Python 脚本运行不正确。无输出

Python 脚本运行不正确。无输出,python,python-3.x,Python,Python 3.x,我正在编写一个应该读取XML文件的代码。但它没有运行,我也找不到问题所在。我想这和我的笔记本电脑有关 我已经试着在正常的python3外壳中运行它,在那里它工作得非常好 import os import xml.etree.ElementTree as ET tree = ET.parse('people.xml') root = tree.getroot() root[0].attrib 它应该输出以下内容:{'name':'Samy'},它在python3 shell中输出,但在脚

我正在编写一个应该读取XML文件的代码。但它没有运行,我也找不到问题所在。我想这和我的笔记本电脑有关

我已经试着在正常的python3外壳中运行它,在那里它工作得非常好

import os 
import xml.etree.ElementTree as ET 

tree = ET.parse('people.xml')
root = tree.getroot()

root[0].attrib
它应该输出以下内容:
{'name':'Samy'}
,它在python3 shell中输出,但在脚本中不工作

XML文件如下所示

<?xml version="1.0"?>

<PEOPLE>
        <Person name="Samy">
                <age>99</age>
                <number>0176293747238</number>
        </Person>
        <Person name="Alkoholik">
                <age>20</age>
                <number>0176234923482</number>
        </Person>
</PEOPLE>

99
0176293747238
20
0176234923482
添加print()函数可获得所需的输出

import xml.etree.ElementTree as ET

XML = 'people.xml'

tree = ET.parse(XML)
root = tree.getroot()

print(root[0].attrib) #{'name': 'Samy'}

for i in root:
    print(i.attrib) #{'name': 'Samy'}
                    #{'name': 'Alkoholik'}

“不行”是什么意思?有任何错误、输出或其他信息吗?如何运行此脚本?没有一个输出运行脚本时使用的命令是什么?您必须使用打印函数:print(root[0].attrib)我尝试使用打印函数,但它仍然没有给出任何结果