简体   繁体   English

Python最能支持哪种结构化文本格式?

[英]What structured text format is the best supported in Python?

This question may be seen as subjective, but I'd like to ask SO users which common structured textual data format is best supported in Python. 这个问题可能被认为是主观的,但是我想问一下SO用户,哪种通用结构化文本数据格式最受Python支持。

My initial choices are: 我最初的选择是:

  • XML XML格式
  • JSON JSON格式
  • and YAML 和YAML

Which of these three is easiest to work with in Python (ie. has the best library support / performance) ... or is there another format that I haven't mentioned that is better supported in Python. 这三种方法中哪一种最容易在Python中使用(即,具有最佳的库支持/性能)……还是有我没有提到的另一种格式在Python中得到更好的支持。

I cannot use a Python only format (eg Pickling) since interop is quite important, but the majority of the code that handles these files will be written in Python, so I'm keen to use a format that has the strongest support in Python. 我不能使用仅Python格式(例如Pickling),因为互操作非常重要,但是处理这些文件的大部分代码都是用Python编写的,因此我热衷于使用在Python中最受支持的格式。

CSV or fixed column text may also be viable for most use cases, however I'd prefer the flexibility of a more scalable format. CSV或固定列文本对于大多数用例也可能可行,但是我更喜欢更具伸缩性的格式的灵活性。

Thank you 谢谢

Note 注意

Regarding interop I will be generating these files initially from Ruby, using Builder , however Ruby will not be consuming these files again. 关于互操作性,我将首先使用Builder通过Ruby从Ruby生成这些文件,但是Ruby不会再次使用这些文件。

I would go with JSON, I mean YAML is awesome but interop with it is not that great. 我会使用JSON,我的意思是YAML很棒,但是与它的互操作性不是那么好。
XML is just an ugly mess to look at and has too much fat. XML看起来很丑陋, 而且脂肪过多。

Python has a built-in JSON module since version 2.6. 从2.6版开始,Python具有内置的JSON模块。

JSON has great python support and it is much more compact than XML (and the API is generally more convenient if you're just trying to dump and load objects). JSON具有强大的python支持,并且比XML更紧凑(如果您只是想转储和加载对象,则API通常更方便)。 There's no out of the box support for YAML that I know of, although I haven't really checked. 我还没有对YAML的开箱即用支持,尽管我还没有真正检查过。 In the abstract I would suggest using JSON due to the low overhead of the format and the wide range of language support, but it does depend a bit on your application - if you're working in a space that already has established applications, the formats they use might be preferable, even if they're technically deficient. 在摘要中,我建议使用JSON,因为该格式的开销很低并且支持多种语言,但是它确实取决于您的应用程序-如果您在已经建立了应用程序的空间中工作,那么格式即使它们在技术上不足,使用它们还是比较可取的。

I think it depends a lot on what you need to do with the data. 我认为这很大程度上取决于您需要处理数据。 If you're going to be building a complex database and doing processing and transformations on it, I suspect you'd be better off with XML. 如果您要建立一个复杂的数据库并对其进行处理和转换,我怀疑使用XML会更好。 I've found the lxml module pretty useful in this regard. 我发现lxml模块在这方面非常有用。 It has full support for standards like xpath and xslt, and this support is implemented in native code so you'll get good performance. 它完全支持xpath和xslt等标准,并且此支持以本机代码实现,因此您将获得良好的性能。

But if you're doing something more simple, then likely you'd be better off to use a simpler format like yaml or json. 但是,如果您做的事情更简单,那么最好使用yaml或json等更简单的格式。 I've heard tell of "json transforms" but don't know how mature the technology is or how developed Python's access to it is. 我听说过有关“ json转换”的内容,但不知道该技术的成熟程度或Python对它的访问程度。

It's pretty much all the same, out of those three. 在这三个之中,几乎都是一样的。 Use whichever is easier to inter-operate with. 使用更易于互操作的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM