Skip to content

Commit

Permalink
option to automatically handle encoding when reading json
Browse files Browse the repository at this point in the history
  • Loading branch information
USM-CHU-FGuyon committed Nov 14, 2023
1 parent 602dc33 commit 31626f9
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions database_processing/dataprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import operator
import json
import shutil
import chardet

import pandas as pd

Expand Down Expand Up @@ -97,9 +98,14 @@ def _med_concept_id_mapping(self):
dic = {ing: m['blended'] for ing, m in self.ohdsi_med.items()}
return pd.Series(dic)

def _read_json(self, pth):
return json.load(open(pth, 'r'))

def _read_json(self, pth, encoding=None):
if encoding is None:
with open(pth, 'rb') as file_binary:
encoding = chardet.detect(file_binary.read())['encoding']

with open(pth, 'r', encoding=encoding) as file:
return json.load(file)

def load(self, pth, verbose=True, **kwargs):
"""
alias for pd.read_parquet
Expand Down

0 comments on commit 31626f9

Please sign in to comment.