Skip to content

Instantly share code, notes, and snippets.

@danielholmstrom
Created July 13, 2015 16:52
Show Gist options
  • Save danielholmstrom/5358e7cf5ca73d36ec23 to your computer and use it in GitHub Desktop.
Save danielholmstrom/5358e7cf5ca73d36ec23 to your computer and use it in GitHub Desktop.
import os
import csv
original_dir = 'first'
new_dir = 'other'
output_dir = 'output'
for (root, dirs, files) in os.walk(original_dir):
for filename in files:
relpath = os.path.join(root, filename)
doc = {}
with open(os.path.join(original_dir, relpath), 'r') as csvfile:
for row in csv.DictReader(csvfile, ['key', 'trans']):
doc[row['key']] = row['trans']
if os.path.exists(os.path.join(new_dir, relpath)):
with open(os.path.join(new_dir, relpath), 'r') as csvfile:
for row in csv.DictReader(csvfile, ['key', 'trans']):
doc[row['key']] = row['trans']
# Write
output_path = os.path.join(output_dir, relpath)
print "Would write to", output_path, doc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment