2015-01-14 73 views
2

在我進入之前,是的,我已經看過它,並且知道YAML不會區分引號和非引號,只是將類型視爲它們。坦率地說,我認爲沒有引號的話會更好,但不幸的是,這不是我所需要的。所以請儘量明白,我看着問題,但仍然需要圍繞我的字符串對象的引號。yaml.dump中的強制引號Python

在我的代碼我有一本字典:

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' 
    ... 

但避風港尚未成功。對於從「不可能」到「只是引用他們周圍」的問題,我已經聽到了不同的意見,正如你所看到的,就我而言,我做不到。

關於如何去做這件事的任何想法?

+0

你試過存儲例如字典中的'repr(var1)',而不是'var1'? – jonrsharpe

+0

是的,我有和奇怪它給了var三個單引號:''''var1''''這不是很漂亮...... – Krin123

回答

1

您可以覆蓋標量的發射器並更改動態發射的值的樣式。根據您擁有的其他標量值,您可能需要執行一些測試來取消設置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) 

並獲得儘可能output¹:

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

使用ruamel.yaml由本人筆者¹這樣做。該包是 PyYAML的增強。您可能可以對後者做類似的事情,但會放棄輸入中的註釋。

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,類型的字典(想排序類型的字典)等