11import copy
22import json
33import os
4+ import polars as pl
45from schemas import enrichment_print_schema
56from utils import (
67 convert_to_dataframe ,
910)
1011import xlsxwriter # type: ignore [import-untyped]
1112
13+ # Deals with list columns data that CSV cannot deal with.
14+ def _stringify_list_columns (df : pl .DataFrame ) -> pl .DataFrame :
15+ """Convert any List-type columns to JSON strings so CSV/Excel can handle them."""
16+ list_cols = [col for col , dtype in zip (df .columns , df .dtypes ) if dtype .base_type () == pl .List ]
17+ if list_cols :
18+ df = df .with_columns (
19+ [pl .col (c ).map_elements (lambda val : json .dumps (val , default = str ), return_dtype = pl .String ).alias (c ) for c in list_cols ]
20+ )
21+ return df
1222
1323def export_to_file (
1424 facilities_data : dict ,
@@ -24,10 +34,12 @@ def export_to_file(
2434 match file_type :
2535 case "xlsx" :
2636 with xlsxwriter .Workbook (full_name , {"remove_timezone" : True }) as wb :
27- _ = writer .write_excel (workbook = wb , include_header = True , autofit = True )
37+ _ = _stringify_list_columns (writer ).write_excel (workbook = wb , include_header = True , autofit = True )
38+ # _ = writer.write_excel(workbook=wb, include_header=True, autofit=True)
2839 case "csv" :
2940 with open (full_name , "w" , newline = "" , encoding = "utf-8" ) as f_out :
30- writer .write_csv (file = f_out , include_header = True )
41+ # writer.write_csv(file=f_out, include_header=True)
42+ _stringify_list_columns (writer ).write_csv (file = f_out , include_header = True )
3143 case "parquet" :
3244 writer .write_parquet (full_name , use_pyarrow = True )
3345 case _:
0 commit comments