1
0
Fork
You've already forked csv_reader
0
An example reading a csv files into a dynamically generated dataclass.
  • Python 100%
2024年02月15日 15:36:28 +01:00
main.py More elegant inspection. 2024年02月15日 15:36:28 +01:00
README.md Adding a README.md. 2024年02月15日 14:15:50 +01:00
testdata.csv Some test data as given 2024年02月15日 14:04:20 +01:00

csv_reader

We have a file testdata.csv:

sep=,
field_a,field_b,field_c
10.0,20.0,30.0
1.0,2.0,3.0

We want to read this file into a frozen dataclass with slots, but the dataclass should have field names from the csv file.

This can be done using make_dataclass.

C = make_dataclass('MyClass',
 [('field_a', float),
 ('field_b', float),
 ('field_c', float),]

is the same as

@dataclass
class C:
 field_a: float
 field_b: float
 field_c: float

The code shows how to do this, sanitizing field names, and typing the dataclass.