我试图用python从XML文件中提取数据.我尝试了以下代码.
from xml.etree.ElementTree import ElementTree
tree = ElementTree()
tree.parse("data_v2.xml")
Run Code Online (Sandbox Code Playgroud)
错误信息:
IOError: [Errno 2] No such file or directory: 'data_v2.xml'.
Run Code Online (Sandbox Code Playgroud)
这不是XML错误.这意味着data_v2.xml不存在 - 系统(操作系统)找不到它.也许这个名字是错的,也许你需要提供完整的路径.
import traceback
# ...
try:
input_fname = "data_v2.xml"
tree.parse(input_fname)
# ...
except IOError:
ex_info = traceback.format_exc()
print('ERROR!!! Cannot parse file: %s' % (input_fname))
print('ERROR!!! Check if this file exists and you have right to read it!')
print('ERROR!!! Exception info:\n%s' % (ex_info))
Run Code Online (Sandbox Code Playgroud)