#!/usr/bin/env python3 import sys import yaml def show_type(d): sys.stderr.write( 'foo {}, '.format( type( d.get('foo') ) ) ) sys.stderr.write( 'bar {}\n'.format( type( d.get('bar') ) ) ) need_decode = lambda s: list( filter( lambda c: ord(c) >= 128, s ) ) my_decode = lambda s: s.decode('utf-8') if need_decode(s) else s if __name__ == "__main__": #s = sys.stdin.read() s = sys.stdin.buffer.read() d = yaml.load(s) show_type(d) #d = dict( map( lambda kv: ( kv[0], kv[1].encode('utf-8') ), d.items() ) ) show_type(d) foo = d.get('foo') bar = d.get('bar') d['hoge'] = foo + ' ' + bar d['fuga'] = 'foo={} bar={}'.format(foo, bar) d['guha'] = '(^_^)'.join( d.values() ) #show_type(d) #d = dict( map( lambda kv: ( kv[0], my_decode(kv[1]) ), d.items() ) ) #show_type(d) s = yaml.dump(d, default_flow_style=False, allow_unicode=True) #sys.stdout.write(s) sys.stdout.buffer.write(s) # EOF