Skip to content

Instantly share code, notes, and snippets.



Last active Aug 29, 2015
What would you like to do?
#!/usr/bin/env python
Translate a CSV document with a header to a YAML document that is a list of maps.
Uses constant memory and keeps field names in the original order.
import sys
import csv
import yaml # requires PyYAML
def read_csv(input):
:param input: stream
:return: iterator of (field name, field value) pairs
csv_reader = csv.reader(input)
fields = next(csv_reader)
for row in csv_reader:
yield zip(fields, row)
def write_yaml(pairs, output):
:param pairs: iterator of (field name, field value) pairs
:param output: stream
yaml_writer = yaml.Dumper(output)
yaml_writer.emit(yaml.SequenceStartEvent(anchor=None, tag=None, implicit=True))
for pair in pairs:
yaml_writer.emit(yaml.MappingStartEvent(anchor=None, tag=None, implicit=True))
for name, value in pair:
yaml_writer.emit(yaml.ScalarEvent(anchor=None, tag=None, implicit=(True, True), value=name))
yaml_writer.emit(yaml.ScalarEvent(anchor=None, tag=None, implicit=(True, True), value=value))
def main(input, output):
write_yaml(read_csv(input), output)
if __name__ == '__main__':
main(sys.stdin, sys.stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment