[英]python, yaml how to parse a string containing apostrophe
I am using python to parse YAML files. 我正在使用python解析YAML文件。
One of the YAML documents contain a dictionary such as follow: YAML文档之一包含一个字典,例如:
scrapers:
results: //article[@class='story ']
This apparently causes a problem because the last apostrophe is preceded by a whitespace. 这显然引起了问题,因为最后一个撇号前面是空白。 If I could remove the whitespace it would solve the problem. 如果我可以删除空白,它将解决问题。 However since it is an xpath I can't. 但是,由于它是xpath,所以我不能。
Anyone knows how I could escape that sequence? 有人知道我怎么能逃脱那个顺序吗? I looked into other SO question, but solution like wrapping the string in "", or using 我调查了其他的SO问题,但是解决方案例如将字符串包装在“”中,或者使用
scrapers:
results: //article[@class='story ']
or 要么
scrapers:>
results: //article[@class='story ']
or 要么
scrapers:
results: //article[@class='story '']
did not work. 不工作。
EDIT: I am trying to open a file containing the above expression with: 编辑:我正在尝试打开包含上述表达式的文件:
import yaml
with open('/home/depot/wintergreen/yaml/scrapers.yml', 'r') as f:
scrapers = yaml.load(f)
However i receive the error: ScannerError: mapping values are not allowed here 但是我收到错误:ScannerError:此处不允许映射值
pointing at the whitespace after story
. 指着story
后的空白。 I have been trying a suggestion offered by an answerer below, ie to create the yaml expression from a python dict. 我一直在尝试下面的回答者提供的建议,即从python dict创建yaml表达式。 This works. 这可行。 I i save the yaml to file and load it back again it also does work. 我将yaml保存到文件中,然后再次加载回它,它也可以正常工作。 However when i create the yaml by typing the exact same characters, then it does not work... 但是,当我通过键入完全相同的字符来创建Yaml时,则它不起作用...
EDIT2: I think the problem stemmed from the fact that i created the yaml file on a window machine and uploaded it on a unix server. EDIT2:我认为问题源于我在窗口计算机上创建yaml文件并将其上传到unix服务器的事实。
It's easy to find the correct YAML format for a structure: create the structure in Python then use yaml.dump
to create the YAML-encoded string: 为结构找到正确的YAML格式很容易:在Python中创建结构,然后使用yaml.dump
创建YAML编码的字符串:
d = {'scrapers': {'results': "//article[@class='story ']"}}
print d
import yaml
print yaml.dump(d, default_flow_style=False)
The result of which is: 其结果是:
{'scrapers': {'results': "//article[@class='story '"}}
scrapers:
results: //article[@class='story ']
That's the correct YAML representation, so if you're having a problem, it's with the parser, not the input text. 这是正确的YAML表示形式,因此,如果您遇到问题,则由解析器决定,而不是输入文本。 If you use the standard yaml
library it should parse fine. 如果您使用标准的yaml
库,则应该可以解析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.