From 75defad3766abe9b28cd0e2eb1828614afdeb6ec Mon Sep 17 00:00:00 2001 From: Daniel Darvas Date: Wed, 24 Jan 2018 20:50:29 +0100 Subject: [PATCH] Updated scripts (encoding fix) --- fill_template.py | 6 +++--- upload_to_zenodo.py | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/fill_template.py b/fill_template.py index d368600..891a172 100644 --- a/fill_template.py +++ b/fill_template.py @@ -9,7 +9,7 @@ def fill_template(template_filename, data_filename): template = template_file.read() # Creating the files based on the template and the data descriptor CSV - with open(data_filename,"r") as csv_file: + with open(data_filename,"r", encoding='utf-8-sig') as csv_file: csv_reader = csv.DictReader(csv_file) for row in csv_reader: output_filename = row["FILENAME"] @@ -20,10 +20,10 @@ def fill_template(template_filename, data_filename): for column in csv_reader.fieldnames: if column != "FILENAME": filled_template = filled_template.replace("{%s}" % column, row[column]) - print(" {%s} = %s" % (column, row[column])) + print((" {%s} = %s" % (column, row[column])).encode().decode('cp850')) # Write to output file - with open(output_filename, "w") as output_file: + with open(output_filename, "w", encoding='utf-8') as output_file: output_file.write(filled_template) diff --git a/upload_to_zenodo.py b/upload_to_zenodo.py index d2ddaf5..f09c627 100644 --- a/upload_to_zenodo.py +++ b/upload_to_zenodo.py @@ -44,11 +44,12 @@ def batch_upload(directory): pdf_file = metadata_file.replace(".json",".pdf") if os.path.isfile(pdf_file): print("Uploading %s & %s" % (metadata_file, pdf_file)) - with codecs.open(metadata_file, 'r', "utf-8") as f: + with codecs.open(metadata_file, 'r', 'utf-8') as f: metadata = f.read() # Re-encoding in order to support UTF-8 inputs - metadata = json.dumps(json.loads(metadata), ensure_ascii=True) - print(metadata) + metadata_json = json.loads(metadata) + metadata = json.dumps(metadata_json, ensure_ascii=True) + #print(metadata) upload(metadata, pdf_file) else: print("The file %s might be a submission metadata file, but %s does not exist." % (metadata_file, pdf_file))