使用Python实时解析XML字符串

2024-09-27 22:35:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用TCP服务器客户端API从eye tracker实现XML字符串的实时解析,用于: 1) 以.csv或其他格式存储它们以供脱机分析。 2) 在屏幕上显示凝视坐标

xml字符串的一个小示例如下所示:

    <REC FPOGX="0.68449" FPOGY="0.81953" FPOGS="1789.73828" FPOGD="0.77747" FPOGID="4894" FPOGV="1" CX="0.95781" CY="0.39074" CS="0" />
    <REC FPOGX="0.68449" FPOGY="0.81953" FPOGS="1789.73828" FPOGD="0.77747" FPOGID="4894" FPOGV="1" CX="0.95781" CY="0.39074" CS="0" />
    <REC FPOGX="0.68449" FPOGY="0.81953" FPOGS="1789.73828" FPOGD="0.77747" FPOGID="4894" FPOGV="1" CX="0.95781" CY="0.39074" CS="0" />
    <REC FPOGX="0.68405" FPOGY="0.81942" FPOGS="1789.73828" FPOGD="0.80640" FPOGID="4894" FPOGV="1" CX="0.95781" CY="0.39074" CS="0" />
    <REC FPOGX="0.68405" FPOGY="0.81942" FPOGS="1789.73828" FPOGD="0.80640" FPOGID="4894" FPOGV="1" CX="0.95781" CY="0.39074" CS="0" />
    <REC FPOGX="0.68405" FPOGY="0.81942" FPOGS="1789.73828" FPOGD="0.80640" FPOGID="4894" FPOGV="1" CX="0.95781" CY="0.39074" CS="0" />

眼睛跟踪器每6.6毫秒发送一个样本(如上所示)。我所看到的大部分内容都是获取静态xml文件并进行解析。然而,在我的例子中,我想实时这样做,这样我就可以读取X,Y凝视坐标(例如FPOGX和FPOGY)并在屏幕上显示它们(为此,我将使用一个实验设计软件)。有什么办法吗

非常感谢


Tags: 字符串服务器屏幕xmlcstcpcxrec
2条回答

如果您将其作为xml字符串接收,只需对数据结构执行ET.fromstring(<xml_string>).attrib

import xml.etree.ElementTree as ET 

def parse_xml(xml_string):
    tree = ET.fromstring(xml_string) # et.parse if you are receiving a file
    return tree.attrib

my_xml = '<REC FPOGX="0.68295" FPOGY="0.82154" FPOGS="1789.73828" FPOGD="0.83740" FPOGID="4894" FPOGV="1" CX="0.95781" CY="0.39074" CS="0" />'

data = parse_xml(my_xml)

print(data)

输出:

{'FPOGX': '0.68295', 'FPOGY': '0.82154', 'FPOGS': '1789.73828', 'FPOGD': '0.83740', 'FPOGID': '4894', 'FPOGV': '1', 'CX': '0.95781', 'CY': '0.39074', 'CS': '0'}

对于csv部分,您可以使用这个append_csv_dict函数(我使用的标准函数):

def append_csv_dict(path, data):
    '''
    Append a csv with a dictionary keys as column headers
    Args:
        path (str): Path to the csv file
        data (dict or list): Dictionary or list(dict) with keys as 
                             column  headers and values as column data
    '''
    with open(path, 'a') as file:
        # set the field names to the keys of the dictionary or keys of the first item
        fieldnames = list(data.keys()) if isinstance(data, dict) else data[0].keys()
        writer = csv.DictWriter(file, fieldnames=fieldnames, lineterminator='\n')
        # write the header if the file is new
        if file.tell() == 0:
            writer.writeheader()
        if isinstance(data, dict):
            # write the row
            writer.writerow(data)
        elif isinstance(data, list):
            # write the rows if it is a list
            writer.writerows(data)
            
# append 10 times to show how it works
for i in range(10):
    append_csv_dict('./data.csv', data)

输出(data.csv):

FPOGX,FPOGY,FPOGS,FPOGD,FPOGID,FPOGV,CX,CY,CS
0.68295,0.82154,1789.73828,0.83740,4894,1,0.95781,0.39074,0
0.68295,0.82154,1789.73828,0.83740,4894,1,0.95781,0.39074,0
0.68295,0.82154,1789.73828,0.83740,4894,1,0.95781,0.39074,0
0.68295,0.82154,1789.73828,0.83740,4894,1,0.95781,0.39074,0
0.68295,0.82154,1789.73828,0.83740,4894,1,0.95781,0.39074,0
0.68295,0.82154,1789.73828,0.83740,4894,1,0.95781,0.39074,0
0.68295,0.82154,1789.73828,0.83740,4894,1,0.95781,0.39074,0
0.68295,0.82154,1789.73828,0.83740,4894,1,0.95781,0.39074,0
0.68295,0.82154,1789.73828,0.83740,4894,1,0.95781,0.39074,0
0.68295,0.82154,1789.73828,0.83740,4894,1,0.95781,0.39074,0

下面的代码应该可以做到这一点:

import xml.etree.ElementTree as ET

xml_string = '<REC FPOGX="0.68295" FPOGY="0.82154" FPOGS="1789.73828" FPOGD="0.83740" FPOGID="4894" FPOGV="1" CX="0.95781" CY="0.39074" CS="0" />'

root = ET.fromstring(xml_string)
my_dictionary = root.attrib

print("X:{}, Y:{}".format(my_dictionary["FPOGX"], my_dictionary["FPOGY"]))

您可以使用xml.etree.ElementTree模块处理字符串。输出将是字典

相关问题 更多 >

    热门问题