datasus_db.dbf

Module with helper functions to handler with *datasus_db.dbf files

 1"""
 2Module with helper functions to handler with *.dbf files
 3"""
 4import os.path as path
 5import polars as pl
 6from dbfread import DBF
 7from .utils import rm
 8
 9
10def read_as_df(filename: str, bytes: bytes, encoding: str = None):
11    tmp_file = path.join(".tmp", path.basename(filename).split(".")[0] + ".dbf")
12
13    with open(tmp_file, "wb") as f:
14        f.write(bytes)
15
16    try:
17        dbf = DBF(tmp_file, encoding=encoding)
18        df = pl.DataFrame(iter(dbf))
19        return df
20    except Exception as e:
21        raise e
22    finally:
23        rm(tmp_file)
def read_as_df(filename: str, bytes: bytes, encoding: str = None):
11def read_as_df(filename: str, bytes: bytes, encoding: str = None):
12    tmp_file = path.join(".tmp", path.basename(filename).split(".")[0] + ".dbf")
13
14    with open(tmp_file, "wb") as f:
15        f.write(bytes)
16
17    try:
18        dbf = DBF(tmp_file, encoding=encoding)
19        df = pl.DataFrame(iter(dbf))
20        return df
21    except Exception as e:
22        raise e
23    finally:
24        rm(tmp_file)