人机可读 - 文件中的结构化数据存储

时间:2022-05-12 17:16:59

I need to store structured file data that is both human and machine readable. I mainly use python to open/edit/read these files. However I may need to use other programs as well.

我需要存储人机和机器可读的结构化文件数据。我主要使用python来打开/编辑/读取这些文件。但是,我可能还需要使用其他程序。

I used to use XML/XPATH. However, the xpath libraries are frail and don't work on most systems withtout a significant amount of frustration. I am tired trying to figure out xpath this I port my scripts to different platform.

我以前使用XML / XPATH。但是,xpath库是虚弱的,并且在大多数系统上都不起作用而且没有太大的挫败感。我很累,试图找出xpath,我将脚本移植到不同的平台。

 me@ubuntu:~/$ program -arg1 "foo" -arg2
   File "/home/me/bin/script.py", line 16, in <module>
     from xml import xpath 
 ImportError: cannot import name xpath

BTW, sudo apt-get install python-xml does not fix this problem.

BTW,sudo apt-get install python-xml无法解决这个问题。

The bottom line is that I am fed up with xml/xpath. I want a solution that will work in all cases, on all platforms, without question!

底线是我厌倦了xml / xpath。我想要一个适用于所有平台的解决方案,毫无疑问!

What can I use? Advice?

我可以用什么?建议吗?

1 个解决方案

#1


1  

xml.etree.elementTree is part of the standard library since Python 2.5. It also contains a basic XPath findall function.

xml.etree.elementTree是自Python 2.5以来标准库的一部分。它还包含一个基本的XPath findall函数。

Alternatively, you can lxml's XPath methods which mimic the interface of xml.etree.ElementTree, but support more advanced XPath expressions. However, lxml needs to be installed as it is not (yet) part of the standard library.

或者,您可以使用lxml的XPath方法来模仿xml.etree.ElementTree的接口,但支持更高级的XPath表达式。但是,需要安装lxml,因为它不是标准库的一部分。

For many tasks, you can also use JSON instead of XML. JSON is easy to parse and traverse with the native functions of your programming language.

对于许多任务,您还可以使用JSON而不是XML。 JSON易于使用编程语言的本机函数进行解析和遍历。

#1


1  

xml.etree.elementTree is part of the standard library since Python 2.5. It also contains a basic XPath findall function.

xml.etree.elementTree是自Python 2.5以来标准库的一部分。它还包含一个基本的XPath findall函数。

Alternatively, you can lxml's XPath methods which mimic the interface of xml.etree.ElementTree, but support more advanced XPath expressions. However, lxml needs to be installed as it is not (yet) part of the standard library.

或者,您可以使用lxml的XPath方法来模仿xml.etree.ElementTree的接口,但支持更高级的XPath表达式。但是,需要安装lxml,因为它不是标准库的一部分。

For many tasks, you can also use JSON instead of XML. JSON is easy to parse and traverse with the native functions of your programming language.

对于许多任务,您还可以使用JSON而不是XML。 JSON易于使用编程语言的本机函数进行解析和遍历。