Skip to content

Instantly share code, notes, and snippets.

@danielholmstrom
Created July 13, 2015 17:12
Show Gist options
  • Save danielholmstrom/bd01e8eca8047a95c05c to your computer and use it in GitHub Desktop.
Save danielholmstrom/bd01e8eca8047a95c05c to your computer and use it in GitHub Desktop.
import os
import csv
from collections import OrderedDict
original_dir = os.path.abspath('original')
new_dir = os.path.abspath('new_translations')
output_dir = os.path.abspath('output')
for (root, dirs, files) in os.walk(original_dir):
for filename in files:
relpath = os.path.join(root, filename)
doc = OrderedDict()
with open(relpath, 'r') as csvfile:
for row in csv.DictReader(csvfile, ['key', 'trans']):
doc[row['key']] = row['trans']
# Check new files
new_path = os.path.join(new_dir, relpath[len(original_dir) + 1:])
if os.path.exists(new_path):
with open(new_path, '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[len(original_dir) + 1:])
output_path_dir = os.path.dirname(output_path)
if not os.path.exists(output_path_dir):
os.makedirs(output_path_dir)
with open(output_path, 'w') as csvfile:
writer = csv.writer(csvfile, delimiter=",", quotechar='"',
quoting=csv.QUOTE_ALL)
for k, v in doc.iteritems():
writer.writerow([k, v])
for (root, dirs, files) in os.walk(new_dir):
for filename in files:
relpath = os.path.join(root, filename)
doc = OrderedDict()
with open(relpath, 'r') as csvfile:
for row in csv.DictReader(csvfile, ['key', 'trans']):
doc[row['key']] = row['trans']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment