Created
July 13, 2015 17:12
-
-
Save danielholmstrom/bd01e8eca8047a95c05c to your computer and use it in GitHub Desktop.
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 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