在yaml.dump Python中强制引用

时间:2015-01-14 16:21:38

标签: python string yaml quotes

在我进入之前,是的,我已经对它进行了调查,并且知道YAML没有区分引号和非引号,只是采用它们的类型。坦率地说,如果没有引号,我觉得它更好,但不幸的是,这不是我需要的。所以请尝试理解我已经调查此事但仍需要我的字符串对象周围的引号。

在我的代码中,我有一本字典:

data = {dic:[{A:''}, {B:''}, {C:''}, {D:''},...]}

'var*'是通过PyQt4 QLineEdit类构造的,代码行提取的行如下所示:

var* = str(QtGui.QLineEdit().displayText())

所以当我做data['dic'][index]['A'-'Z'] = var*

变为data = {dic:[{A: 'var1'}, {B:'var2'}, {C:'var3'}, {D:'var4'},...]}

然后我转储了所有数据:

prettyData = yaml.dump(data, default_flow_style=False, width=10000000)

print prettyData我明白了:

dic:
  - A: var1
  - B: var2
  - C: var3
  - D: var4
  ...

我尝试了很多东西让它们出现:

dic:
  - A: 'var1'
  - B: 'var2'
  - C: 'var3'
  - D: 'var4'
  ...

但尚未成功。我已经听过不同的意见,包括"不可能" to"只是在它们周围加上引号",正如你所看到的,在我的情况下,我不能这样做。

关于如何解决这个问题的任何想法?

2 个答案:

答案 0 :(得分:2)

您可以覆盖标量的发射器并更改动态发射的值的样式。根据您拥有的其余标量值,您可能需要执行一些更多测试来取消设置is_string。在调用process_scalar时,您不再知道原始值,只需要一个带有(unicode)字符串值的事件。

import sys
import ruamel.yaml as yaml

yaml_str = """\
dic:
  - A: var1    # need to get these strings quoted
  - B: var2
  - C: var3
  - D: var4
  - E: 3       # leave this alone
"""

# the scalar emitter from emitter.py
def process_scalar(self):
    if self.analysis is None:
        self.analysis = self.analyze_scalar(self.event.value)
    if self.style is None:
        self.style = self.choose_scalar_style()
    split = (not self.simple_key_context)
    # VVVVVVVVVVVVVVVVVVVV added
    if split:  # not a key
        is_string = True
        if self.event.value and self.event.value[0].isdigit():
            is_string = False
        # insert extra tests for scalars that should not be ?
        if is_string:
            self.style = "'"
    # ^^^^^^^^^^^^^^^^^^^^
    # if self.analysis.multiline and split    \
    #         and (not self.style or self.style in '\'\"'):
    #     self.write_indent()
    if self.style == '"':
        self.write_double_quoted(self.analysis.scalar, split)
    elif self.style == '\'':
        self.write_single_quoted(self.analysis.scalar, split)
    elif self.style == '>':
        self.write_folded(self.analysis.scalar)
    elif self.style == '|':
        self.write_literal(self.analysis.scalar)
    else:
        self.write_plain(self.analysis.scalar, split)
    self.analysis = None
    self.style = None
    if self.event.comment:
        self.write_post_comment(self.event)

data = yaml.load(yaml_str, Loader=yaml.RoundTripLoader)
dd = yaml.RoundTripDumper
dd.process_scalar = process_scalar
yaml.dump(data, sys.stdout, Dumper=dd)

并获得输出¹:

dic:
- A: 'var1'    # need to get these strings quoted
- B: 'var2'
- C: 'var3'
- D: 'var4'
- E: 3         # leave this alone

¹这是使用ruamel.yaml完成的,我是作者。那个包是 PyYAML的增强。您可以使用后者执行类似操作,但会删除输入中的注释。

答案 1 :(得分:0)

#!/usr/bin/env python2
# quoting_example.py
import yaml
import yaml.representer

def literal_presenter(dumper, data):
  if isinstance(data, str) and "\n" in data:
      return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
  return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='"')

yaml.add_representer(str, literal_presenter)
print yaml.dump({"key": "value", "multiline": "foo\nbar"}, default_flow_style=False)

结果是:

$ python2 quoting_example.py
"key": "value"
"multiline": |-
  foo
  bar

同样,您可以为unicode,dicts(思考排序的dicts)等添加代表。