Created
August 12, 2013 04:05
-
-
Save kates/6208191 to your computer and use it in GitHub Desktop.
Double quoting YAML keys and scalar values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
from yaml import load, dump, add_representer | |
try: | |
from yaml import CDumper as Dumper | |
except ImportError: | |
from yaml import Dumper | |
def rep(dumper, data): | |
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style=None) | |
def process_file(file): | |
add_representer(unicode, rep) | |
with open(file, 'r') as f: | |
doc = load(f.read(), Loader=Loader) | |
yml = dump(doc, | |
default_flow_style=False, | |
allow_unicode=True, | |
default_style='"', | |
line_break=False, width=10000000 | |
) | |
print yml | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Stringify YAML') | |
parser.add_argument('file') | |
args = parser.parse_args() | |
process_file(args.file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment