简体   繁体   English

如何使用 Python 读取 xer 文件?

[英]How to read xer-files using Python?

I have some Primavera .xer files.我有一些 Primavera .xer 文件。 I can open these files in notepad and I see, that there is some text data (it looks like database data).我可以在记事本中打开这些文件,我看到有一些文本数据(它看起来像数据库数据)。

Are there any Python parsers for these files?这些文件是否有任何 Python 解析器?

The Primavera P6 XER format is indeed vulnerable to analysis by the CSV module. Primavera P6 XER 格式确实容易受到 CSV 模块的分析。 It's basically a tab-delimited back-up format for a relational database.它基本上是关系数据库的制表符分隔的备份格式。

The first row is some meta-information about the file.第一行是关于文件的一些元信息。 Thereafter, each row begins with a code that tells what the rest of the row contains:此后,每一行都以一个代码开头,该代码说明该行的其余部分包含的内容:

  • %T gives the name of a table. %T 给出表的名称。
  • %F gives the name of some fields in that table, all on one line. %F 给出了该表中某些字段的名称,全部在一行中。
  • %R is a record for the most recently-defined table. %R 是最近定义的表的记录。
  • %E is the end-of-file marker. %E 是文件结束标记。

The rest depends entirely on how you want to use the data.其余的完全取决于您想如何使用数据。 For my part, I put the data directly into a (fresh, new) SQLITE database first and worry about applications later.就我而言,我首先将数据直接放入(全新的)SQLITE 数据库中,然后再考虑应用程序。

Not an ideal solution, but you could try using MPXJ.不是理想的解决方案,但您可以尝试使用 MPXJ。 MPXJ itself is a Java library, but assuming you can launch a separate process from your Python code you can get it to convert the XER file to JSON which should be easier to consume (this is the approach used by the MPXJ Ruby gem, it's just wrapping the Java code performing the conversion, then reading the resulting JSON). MPXJ 本身是一个 Java 库,但假设您可以从 Python 代码中启动一个单独的进程,您可以使用它来将 XER 文件转换为 JSON,这应该更容易使用(这是 MPXJ Ruby gem 使用的方法,它只是包装执行转换的 Java 代码,然后读取生成的 JSON)。 You should also find the structure of the schedule data in the JSON generated by MPXJ easier to work with than the contents of the XER file, although you won't have access to every attribute in the original XER file.您还应该发现 MPXJ 生成的 JSON 中的计划数据结构比 XER 文件的内容更易于使用,尽管您无法访问原始 XER 文件中的每个属性。

The ruby version of this process can be found here .可以在此处找到此过程的 ruby​​ 版本。 To summarise, the command line will look something like this:总而言之,命令行将如下所示:

java -cp <classpath> net.sf.mpxj.sample.MpxjConvert your-file.xer your-file.json

You'll just need to set supply the <classpath> to tell the JVM where MPXJ JAR and its dependencies are.您只需要设置提供<classpath>来告诉 JVM MPXJ JAR 及其依赖项的位置。

I know it is a bit late.我知道有点晚了。 However, I have been looking for a similar answer and I couldn't.但是,我一直在寻找类似的答案,但我找不到。 So, I have started the development of an opensource package called PyP6XER.所以,我开始开发一个名为 PyP6XER 的开源包。 You can pip install it or contribute to the development on github https://github.com/HassanEmam/PyP6Xer您可以 pip install 它或在 github https://github.com/HassanEmam/PyP6Xer上为开发做出贡献

Note the package is still in its early stages and not well documented.请注意,该软件包仍处于早期阶段并且没有很好的文档记录。

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

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