0

我正在尝试解析XML从 Bricklink 网站导出的一个或多个文件。我通过defusedxmlElementTree 系统打开文件。

我尝试打开的每个文件都会在尝试打开时让我收到OSError 22,即使文件名看似有效 - 我可以使用其他工具打开文件并查看内容。

痕迹是:

Traceback (most recent call last):
  File "C:\Users\Jason\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "D:/Jason/Projects/Projects/wantedlist_modder.py", line 48, in processfiles
    tree = ET.parse(filename)
  File "C:\Users\Jason\AppData\Local\Programs\Python\Python38\lib\site-packages\defusedxml\common.py", line 105, in parse
    return _parse(source, parser)
  File "C:\Users\Jason\AppData\Local\Programs\Python\Python38\lib\xml\etree\ElementTree.py", line 1202, in parse
    tree.parse(source, parser)
  File "C:\Users\Jason\AppData\Local\Programs\Python\Python38\lib\xml\etree\ElementTree.py", line 584, in parse
    source = open(source, "rb")
OSError: [Errno 22] Invalid argument: "'D:/Jason/Downloads/LF_LtN_2020.xml'"

python代码如下:

from tkinter import messagebox, filedialog
import tkinter as tk
from defusedxml.ElementTree import *
import defusedxml.ElementTree as ET
import io

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.pack(padx = 20, pady = 20)
        frame = Frame(self)
        frame.pack(side = TOP)
        # create variable for input field
        self.filename = tk.StringVar()
        self.filename.set("")
        # create input field
        self.inputfile = tk.Entry(frame)
        self.inputfile.pack(side = LEFT)
        self.inputfile["textvariable"] = self.filename
        # create browse button
        self.browse = tk.Button(frame, text = "Browse...", command = self.openbrowse)
        self.browse.pack(side = LEFT)
        # create process button
        self.process = tk.Button(self, text = "Process", command = self.processfiles)
        self.process.pack(side = TOP, padx = 20, pady = 20)
        # create quit button
        self.quit = tk.Button(self, text = "QUIT", fg = "red", command = self.master.destroy)
        self.quit.pack(side = BOTTOM)

    def openbrowse(self):
        filenames = tk.filedialog.askopenfilenames(title = 'select', filetypes = [("XML", ".xml"),])
        if not filenames is None:
            self.filename.set(', '.join(repr(u) for u in filenames))

    def processfiles(self):
        print("Roger, roger!")
        filenames = tuple(self.filename.get().split(','))
        showdone = 0
        for filename in filenames:
            try:
                print(filename)
                tree = ET.parse(filename)
                showdone = 1
            except FileNotFoundError:
                tk.messagebox.showinfo("Wanted List Modder", "Please enter a valid filename")
        if showdone:
            tk.messagebox.showinfo("Wanted List Modder", "Done!")

root = tk.Tk()
app = Application(master=root)
app.mainloop()

我已经做了很多尝试来解决这个问题,但我在这个网站或其他地方找到的修复候选者似乎都没有改变正在发生的事情(例如,我从文件而不是字符串解析)。我不确定内容是否有问题,这似乎与文件名有关 - 或者我将其传递给解析函数的方式。它确实打印了它正在处理的文件的名称,我正在使用浏览按钮系统来定位文件。我知道这会导致一些额外的处理转换为元组/从元组转换,但我认为这不是罪魁祸首,因为传递给 parse 的变量值是一个字符串。

在机器上运行Python 3.8.6...并且非常希望帮助确定 OSError 的原因。defusedxml 0.6.0Windows 7-x64

在此先感谢-如果它很简单,我们深表歉意!我有一段时间没有使用Python了,我正在与化疗的不良神经系统影响作斗争......

4

1 回答 1

1

"见下文 -文件名中有额外内容

# your code with a bug
try:
    with open('"c:/temp/a.xml"', 'rb') as f:
        pass
except OSError:
    print('we have a problem')

# bug was removed - it works
with open('c:/temp/a.xml', 'rb') as f:
    print('it works')
于 2020-10-31T14:46:51.213 回答