在 python 中向后打印 XML 属性

发布于 2025-01-15 00:45:54 字数 2317 浏览 2 评论 0原文

当我使用此代码获得 XML 文件的打印输出时,它会向后打印,如下所示。


import os
import xml.etree.cElementTree as ET
import pprint

data_directory = os.environ.get('')


def taxRules():
    fileName = '/taxRules.xml'
    tree = ET.parse(data_directory + fileName)
    root = tree.getroot()

    data_list = []

    for child in root.iter("taxRule"):
        data = child.attrib.copy()

       # data['status'] = (data['status'])
       # data['rate'] = float(data['rate'])
       # data['range1'] = float(data['range1'])
        data_list.append(data)

    return data_list


if __name__ == '__main__':
    results = taxRules()
    pprint.pprint(results)

这是打印输出,但它应该打印出来 status、rate、range1、range2

知道如何让它准确打印出 XML 文件中显示的内容吗?

我正在使用 python 3.8


[{'range1': '0', 'range2': '9950', 'rate': '0.1', 'status': 's'},
 {'range1': '9950', 'range2': '40525', 'rate': '0.12', 'status': 's'},
 {'range1': '40525', 'range2': '86375', 'rate': '0.22', 'status': 's'},
 {'range1': '86375', 'range2': '164925', 'rate': '0.24', 'status': 's'},
 {'range1': '164925', 'range2': '209425', 'rate': '0.32', 'status': 's'},
 {'range1': '209425', 'range2': '523600', 'rate': '0.35', 'status': 's'},
 {'range1': '523600', 'range2': 'max', 'rate': '0.37', 'status': 's'},
 {'range1': '0', 'range2': '19900', 'rate': '0.1', 'status': 'mfj'},
 {'range1': '19900', 'range2': '81050', 'rate': '0.12', 'status': 'mfj'},
 {'range1': '81050', 'range2': '172750', 'rate': '0.22', 'status': 'mfj'},
 {'range1': '172750', 'range2': '329850', 'rate': '0.24', 'status': 'mfj'},
 {'range1': '329850', 'range2': '418850', 'rate': '0.32', 'status': 'mfj'},
 {'range1': '418850', 'range2': '628300', 'rate': '0.35', 'status': 'mfj'},
 {'range1': '628300', 'range2': 'max', 'rate': '0.37', 'status': 'mfj'},
 {'range1': '0', 'range2': '14200', 'rate': '0.1', 'status': 'hh'},
 {'range1': '14200', 'range2': '54200', 'rate': '0.12', 'status': 'hh'},
 {'range1': '54200', 'range2': '86350', 'rate': '0.22', 'status': 'hh'},
 {'range1': '86350', 'range2': '164900', 'rate': '0.24', 'status': 'hh'},
 {'range1': '164900', 'range2': '209400', 'rate': '0.32', 'status': 'hh'},
 {'range1': '209400', 'range2': '523600', 'rate': '0.35', 'status': 'hh'},
 {'range1': '523600', 'range2': 'max', 'rate': '0.37', 'status': 'hh'}]

When I get a printout of the XML file using this code, it prints backward as shown below.


import os
import xml.etree.cElementTree as ET
import pprint

data_directory = os.environ.get('')


def taxRules():
    fileName = '/taxRules.xml'
    tree = ET.parse(data_directory + fileName)
    root = tree.getroot()

    data_list = []

    for child in root.iter("taxRule"):
        data = child.attrib.copy()

       # data['status'] = (data['status'])
       # data['rate'] = float(data['rate'])
       # data['range1'] = float(data['range1'])
        data_list.append(data)

    return data_list


if __name__ == '__main__':
    results = taxRules()
    pprint.pprint(results)

this is the printout but its suppose to print out
status, rate, range1, range2

Any idea how I can get it to print out exactly how it shown in the XML file?

I'm using python 3.8


[{'range1': '0', 'range2': '9950', 'rate': '0.1', 'status': 's'},
 {'range1': '9950', 'range2': '40525', 'rate': '0.12', 'status': 's'},
 {'range1': '40525', 'range2': '86375', 'rate': '0.22', 'status': 's'},
 {'range1': '86375', 'range2': '164925', 'rate': '0.24', 'status': 's'},
 {'range1': '164925', 'range2': '209425', 'rate': '0.32', 'status': 's'},
 {'range1': '209425', 'range2': '523600', 'rate': '0.35', 'status': 's'},
 {'range1': '523600', 'range2': 'max', 'rate': '0.37', 'status': 's'},
 {'range1': '0', 'range2': '19900', 'rate': '0.1', 'status': 'mfj'},
 {'range1': '19900', 'range2': '81050', 'rate': '0.12', 'status': 'mfj'},
 {'range1': '81050', 'range2': '172750', 'rate': '0.22', 'status': 'mfj'},
 {'range1': '172750', 'range2': '329850', 'rate': '0.24', 'status': 'mfj'},
 {'range1': '329850', 'range2': '418850', 'rate': '0.32', 'status': 'mfj'},
 {'range1': '418850', 'range2': '628300', 'rate': '0.35', 'status': 'mfj'},
 {'range1': '628300', 'range2': 'max', 'rate': '0.37', 'status': 'mfj'},
 {'range1': '0', 'range2': '14200', 'rate': '0.1', 'status': 'hh'},
 {'range1': '14200', 'range2': '54200', 'rate': '0.12', 'status': 'hh'},
 {'range1': '54200', 'range2': '86350', 'rate': '0.22', 'status': 'hh'},
 {'range1': '86350', 'range2': '164900', 'rate': '0.24', 'status': 'hh'},
 {'range1': '164900', 'range2': '209400', 'rate': '0.32', 'status': 'hh'},
 {'range1': '209400', 'range2': '523600', 'rate': '0.35', 'status': 'hh'},
 {'range1': '523600', 'range2': 'max', 'rate': '0.37', 'status': 'hh'}]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情未る 2025-01-22 00:45:55

它是一本字典,因此顺序并不重要,因为您将通过其键访问它。但如果您仍然想按顺序打印它,您可以使用此代码重新排列它。

reorder_dict = [{x: d[x] for x in ["status", "range1", "range2", "range3", "rate"]} for d in data_list]

将这个想法扩展到您的 taxRules() 方法,您可能会这样做:

def taxRules():
    fileName = '/taxRules.xml'
    tree = ET.parse(data_directory + fileName)
    root = tree.getroot()

    data_list = [
        {
            key: child.attrib[key]
            for key
            in ["status", "rate", "range1", "range2"]
        }
        for child
        in root.iter("taxRule")
    ]

    return data_list

It is a dictionary, so the order is not really important since you will access it by its key. But if you still want to print it in order, you can re-arrange it using this code.

reorder_dict = [{x: d[x] for x in ["status", "range1", "range2", "range3", "rate"]} for d in data_list]

Extending this idea to your taxRules() method, you might do:

def taxRules():
    fileName = '/taxRules.xml'
    tree = ET.parse(data_directory + fileName)
    root = tree.getroot()

    data_list = [
        {
            key: child.attrib[key]
            for key
            in ["status", "rate", "range1", "range2"]
        }
        for child
        in root.iter("taxRule")
    ]

    return data_list
花想c 2025-01-22 00:45:55

它无序打印 XML 文件的原因是因为我使用的是 pprint.pprint 而不是我用它来在列表视图中获取我想要的打印......

if __name__ == '__main__':
    values = taxRules()
    for v in values:
        print(v)

The reason why it was printing the XML file unordered was because I was using pprint.pprint instead I used this to get the print I wanted in list view ....

if __name__ == '__main__':
    values = taxRules()
    for v in values:
        print(v)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文