enforce utf-8 encoding for reading/writing JSON

This commit is contained in:
James Powell
2018-10-29 11:14:31 -04:00
parent cefd1cf030
commit bcf93e1d9f
8 changed files with 17 additions and 17 deletions

View File

@@ -70,7 +70,7 @@ def read_excel(input_filename):
def create_eqt_template(links,nodes, links_by_src , links_by_dest, input_filename):
output_filename = f'{input_filename[:-4]}_eqpt_sheet.txt'
with open(output_filename,'w') as my_file :
with open(output_filename, 'w', encoding='utf-8') as my_file:
# print header similar to excel
my_file.write('OPTIONAL\n\n\n\
\t\tNode a egress amp (from a to z)\t\t\t\t\tNode a ingress amp (from z to a) \

View File

@@ -74,7 +74,7 @@ def load_requests(filename,eqpt_filename):
logger.info('Automatically converting requests from XLS to JSON')
json_data = convert_service_sheet(filename,eqpt_filename)
else:
with open(filename) as f:
with open(filename, encoding='utf-8') as f:
json_data = loads(f.read())
return json_data

View File

@@ -26,8 +26,8 @@ parser.add_argument('eqpt_filename', nargs='?', type = Path, default=Path(__file
if __name__ == '__main__':
args = parser.parse_args()
with open(args.output_filename,"w") as file :
with open(args.filename) as f:
with open(args.output_filename, 'w', encoding='utf-8') as file:
with open(args.filename, encoding='utf-8') as f:
print(f'Reading {args.filename}')
json_data = loads(f.read())
equipment = load_equipment(args.eqpt_filename)

View File

@@ -220,7 +220,7 @@ def convert_file(input_filename, filter_region=[]):
full_input_filename = str(input_filename)
split_filename = [full_input_filename[0:len(full_input_filename)-len(suffix_filename)] , suffix_filename[1:]]
output_json_file_name = split_filename[0]+'.json'
with open(output_json_file_name,'w') as edfa_json_file:
with open(output_json_file_name, 'w', encoding='utf-8') as edfa_json_file:
edfa_json_file.write(dumps(data, indent=2))
return output_json_file_name

View File

@@ -43,13 +43,13 @@ class Amp(AmpBase):
@classmethod
def from_advanced_json(cls, filename, **kwargs):
with open(filename) as f:
with open(filename, encoding='utf-8') as f:
json_data = load(f)
return cls(**{**kwargs, **json_data, 'type_def':None, 'nf_model':None})
@classmethod
def from_default_json(cls, filename, **kwargs):
with open(filename) as f:
with open(filename, encoding='utf-8') as f:
json_data = load(f)
type_variety = kwargs['type_variety']
type_def = kwargs.get('type_def', 'variable_gain') #default compatibility with older json eqpt files

View File

@@ -18,13 +18,13 @@ from scipy import constants
def load_json(filename):
with open(filename, 'r') as f:
with open(filename, 'r', encoding='utf-8') as f:
data = json.load(f)
return data
def save_json(obj, filename):
with open(filename, 'w') as f:
with open(filename, 'w', encoding='utf-8') as f:
json.dump(obj, f, indent=2)
def write_csv(obj, filename):
@@ -55,7 +55,7 @@ def write_csv(obj, filename):
result_category 2
...
"""
with open(filename, 'w') as f:
with open(filename, 'w', encoding='utf-8') as f:
w = writer(f)
for data_key, data_list in obj.items():
#main header

View File

@@ -105,16 +105,16 @@ def encode_sets(obj):
if __name__ == '__main__':
args = parser.parse_args()
with open(args.expected_output) as f:
with open(args.expected_output, encoding='utf-8') as f:
expected = load(f)
with open(args.actual_output) as f:
with open(args.actual_output, encoding='utf-8') as f:
actual = load(f)
result = COMPARISONS[args.comparison](expected, actual)
if args.output:
with open(args.output, 'w') as f:
with open(args.output, 'w', encoding='utf-8') as f:
dump(result, f, default=encode_sets, indent=2)
else:
print(str(result))

View File

@@ -37,11 +37,11 @@ def test_excel_json_generation(xls_input, expected_json_output):
convert_file(xls_input)
actual_json_output = xls_input.with_suffix('.json')
with open(actual_json_output) as f:
with open(actual_json_output, encoding='utf-8') as f:
actual = load(f)
unlink(actual_json_output)
with open(expected_json_output) as f:
with open(expected_json_output, encoding='utf-8') as f:
expected = load(f)
results = compare_networks(expected, actual)
@@ -65,11 +65,11 @@ def test_excel_service_json_generation(xls_input, expected_json_output):
convert_service_sheet(xls_input, eqpt_filename)
actual_json_output = f'{str(xls_input)[:-4]}_services.json'
with open(actual_json_output) as f:
with open(actual_json_output, encoding='utf-8') as f:
actual = load(f)
unlink(actual_json_output)
with open(expected_json_output) as f:
with open(expected_json_output, encoding='utf-8') as f:
expected = load(f)
results = compare_services(expected, actual)