title: Pyyaml - Using different styles for keys and integers and strings
body: <pre><code>--- 
  "main": 
    "directory": 
      "options": 
        "directive": 'options'
        "item": 
          "options": 'Stuff OtherStuff MoreStuff'
    "directoryindex": 
      "item": 
        "directoryindex": 'stuff.htm otherstuff.htm morestuff.html'
    "fileetag": 
      "item": 
        "fileetag": 'Stuff'
    "keepalive": 
      "item": 
        "keepalive": 'Stuff'
    "keepalivetimeout": 
      "item": 
        "keepalivetimeout": 2
  </code></pre>
  
  <p>above is a YAML file which I need to parse, edit then dump. I have chosen to do so with pyyaml on python 2.7 (I need to use this).
  I have been able to parse and edit. </p>
  
  <p>However, since the YAML has different styles for keys and different styles for strings and integers I cannot set a default style. I am now wondering how I can use pyyaml to dump different styles for the different types.</p>
  
  <p>Below is what I do to parse and edit</p>
  
  <pre><code>infile = yaml.load(open('yamlfile'))
  
  #Recursive function to loop through nested dictionary
  def edit(d,keytoedit=None,newvalue=None):
    for key, value in d.iteritems():
      if isinstance(value, dict) and key == keytoedit and 'item' in value:
        value[value.iterkeys().next()] = {keytoedit:newvalue}
        edit(value,keytoedit=keytoedit,newvalue=newvalue)
      elif isinstance(value, dict) and keytoedit in value and 'item' not in value and key != 'main':
        value[keytoedit] = newvalue
        edit(value,keytoedit=keytoedit,newvalue=newvalue)
      elif isinstance(value, dict):
        edit(value,keytoedit=keytoedit,newvalue=newvalue)
  
  outfile = file('outfile','w')
  yaml.dump(infile, outfile,default_flow_style=False)
  </code></pre>
  
  <p>So, I am wondering how I can achieve that, if I use the default_style in yaml.dump all the types get the same style and I need to adhere to the original YAML files standard.</p>
  
  <p>Can I somehow specify styles for specific types with pyyaml?</p>
  
  <p>Edit:
  Here is what i get so far, the missing piece is the double qoutes on the keys and the single qoutes on the strings.</p>
  
  <pre><code>main:
    directory:
      options:
        directive: options
        item:
          options: Stuff OtherStuff MoreStuff
    directoryindex:
      item:
        directoryindex: stuff.html otherstuff.htm morestuff.html
    fileetag:
      item:
        fileetag: Stuff
    keepalive:
      item:
        keepalive: 'On'
    keepalivetimeout:
      item:
        keepalivetimeout: 2
  </code></pre>