Skip to content
Snippets Groups Projects

Try several encodings to read old WEO releases

Closed Pierre Dittgen requested to merge flex_csv_read into master
2 unresolved threads
1 file
+ 11
1
Compare changes
  • Side-by-side
  • Inline
+ 11
1
@@ -60,6 +60,16 @@ def unit_slugify(unit_label):
return slugify(lower_unit_label, separator="_", stopwords=["of"])
def dataframe_from_csv(csv_file: Path, encoding_list):
"""Try several encodings to read csv content."""
for encoding in encoding_list:
try:
return pd.read_csv(csv_file, delimiter="\t", encoding=encoding)
except UnicodeDecodeError:
Please register or sign in to reply
pass
raise ValueError(f"Can't find an encoding to read {csv_file!r}")
Please register or sign in to reply
def process_csv_file(
csv_file: Path, area_info: Dict[str, Any], year_month: Tuple[str, str]
):
@@ -73,7 +83,7 @@ def process_csv_file(
"unit": {},
}
df = pd.read_csv(csv_file, delimiter="\t")
df = dataframe_from_csv(csv_file, ["utf-8", "iso-8859-1"])
# Remove unused lines
df = df[df["WEO Subject Code"].notnull()]