- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试读取 spss 文件后面的 xml,我想从 etree 转移到 objectify。
如何将下面的函数转换为返回一个对象化对象?我想这样做是因为 objectify xml 对象对我(作为新手)来说更容易使用,因为它更像 pythonic。
def get_etree(path_file):
from lxml import etree
with open(path_file, 'r+') as f:
xml_text = f.read()
recovering_parser = etree.XMLParser(recover=True)
xml = etree.parse(StringIO(xml_text), parser=recovering_parser)
return xml
我失败的尝试:
def get_etree(path_file):
from lxml import etree, objectify
with open(path_file, 'r+') as f:
xml_text = objectify.fromstring(xml)
return xml
但是我得到这个错误:
lxml.etree.XMLSyntaxError: xmlns:mdm: 'http://www.spss.com/mr/dm/metadatamodel/Arc 3/2000-02-04' is not a valid URI
最佳答案
第一个也是最大的错误是将文件读入字符串并将该字符串提供给 XML 解析器。
Python 将按照您的默认文件编码读取文件(除非您在调用 read()
时指定编码),并且该步骤很可能会破坏除纯 ASCII 文件以外的任何内容。
XML 文件有多种编码方式,您无法预测它们,您真的不应该对它们做出假设。 XML 文件通过 XML 声明解决了这个问题。
<?xml version="1.0" encoding="Windows-1252"?>
XML 解析器将读取该位信息并正确配置自身,然后再读取文件的其余部分。利用那个设施。 切勿对 XML 文件使用 open()
和 read()
。
幸运的是 lxml 使它变得非常简单:
from lxml import etree, objectify
def get_etree(path_file):
return etree.parse(path_file, parser=etree.XMLParser(recover=True))
def get_objectify(path_file):
return objectify.parse(path_file)
和
path = r"/path/to/your.xml"
xml1 = get_etree(path)
xml2 = get_objectify(path)
print xml1 # -> <lxml.etree._ElementTree object at 0x02A7B918>
print xml2 # -> <lxml.etree._ElementTree object at 0x02A7B878>
P.S.:如果您确实肯定地必须使用恢复解析器,请认真考虑。 XML 文件是一种数据结构。如果它损坏了(语法无效、不完整、解码错误,随便你怎么说),你真的愿意相信(根据定义未定义)尝试读取它的结果,还是宁愿拒绝它并显示一条错误消息?
我会选择后者。使用恢复解析器可能会在以后导致严重的运行时错误。
关于python - 用 python 解析 lxml : how to with objectify,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27276158/
我正在像这样使用 Objectify 保存一个对象: Thing th = new Thing(); th.identifier = thingId; th.name = thingName; th.
我正在像这样使用 Objectify 保存一个对象: Thing th = new Thing(); th.identifier = thingId; th.name = thingName; th.
是否可以通过仅返回特定列表字段为空的实体来过滤 objectify 中的实体? 例如,如果我有一个客户端实体,它有一个属性“地址”,属于列表类型,我如何才能只返回根本没有关联地址的客户端? 是否有某种
我正在使用 Google App engine1.9.3、Eclipse、Objectify5.03。我的类(class)如下: import com.googlecode.objectify.Ref
我从 http://code.google.com/p/objectify-appengine/downloads/list 下载了 Objectify 3.1 库,当我遵循指南时,他们说我应该用 @
我已经阅读了一些文档,但还无法与数据存储区进行通信……谁能给我一个在 GWT Web 应用程序中使用的 objectify 的示例项目/代码(我使用 eclipse)……只是一个简单的“使用 RPC
我正在尝试将 Objectify 与一组从抽象基类继承的类一起使用: class Container { @Id Long id; @Embedded Set children = new H
我正在使用 Ojectify 来存储数据存储区,我有一个实体,当我保存更改时保留它,但在浏览器中显示它有时会显示以前的数据,有时会显示新数据。 @Entity public class BrandDt
此查询有效,fullPath字段是 List : KeyLookup lookup = ofy().load().type(KeyLookup.class).filter("f
作为 Google Cloud Datastore 的新手,我想确保自己走在正确的道路上。 我需要什么: 多对多关系 关系本身必须保存描述关系的数据 两种方式都需要强一致性: 从用户实体到该用户有权访
考虑更复杂的实体结构,例如 class Entity { Float valueA; Float valueB; List properties; } class Proper
我在 Google App Engine 上使用 Objectify 5.1.1。我定义了一个名为 Insight 的 Objectify 实体,并尝试通过名为 Downloaded 的 boolea
我有三个实体:人类、狗、零食。 Treat 实体拥有 key Key ,并且 Dog 实体拥有 key Key 。当我查询 Treat 时,我想要一个包含 Dog 的实际实体的响应,并且 Dog 的实
sample Objectify code显示以默认可见性声明的实体字段,例如, public class Car { @Id Long id; String vin; int
我需要获取 datePublished IS NOT "" 的项目列表。但是,下面的代码不起作用。有任何想法吗?谢谢 Query q=ofy.query(Diagram.class).filter("
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我正在尝试有效地获取给定父实体的所有子实体。现在唯一的方法是在 objectify 中使用查询,但效率不高,因为它绕过了缓存。 Objectify-4 添加了混合查询,您可以在 Objectify 3
我正在尝试使用谷歌云端点和对象化找到一些具体示例。我已经找到了一些具有端点或对象化的,但没有一个将它们结合在一起。 最佳答案 当我开始学习 objectify 和 endpoints 时遇到了和你一样
当应用程序存储两种实体时: com.mycompany.kind.Model 和 com.mycompany.otherkind.Model 这将如何以相同类型的“模型”存储在数据存储集合中?或者不是
我想执行一个查询,获取结果,然后将光标移至下一项(如果有)。我发现的唯一相关帖子是:Objectify paging with Cursors 有没有一种方法可以在不迭代项目的情况下执行此操作? Qu
我是一名优秀的程序员,十分优秀!