fastiot.util.csv_reader module¶
- class fastiot.util.csv_reader.CSVReader(filename, required_fields=None, optional_fields=None, checks=None, do_allow_arbitrary_fields=False)[source]¶
A class for csv file reading. It uses python-lib’s csv package and does more strict checks on how the csv file must be formatted. Raises a CSVError if the file is incorrectly formatted.
Example usage:
- with CSVReader(‘my_file.csv’,
required_fields=[‘my_field1’, ‘my_field2’], optional_fields=[‘my_optional_field2’]) as reader:
- for data_row in reader:
print(data_row[‘my_field1’]) print(data_row[‘my_field2’]) print(data_row.get(‘my_optional_field2’, ‘unset’))
- __init__(filename, required_fields=None, optional_fields=None, checks=None, do_allow_arbitrary_fields=False)[source]¶
Constructor for csv reader.
- Parameters:
filename (
str
) – The filename of the csv file. Must be relative to workdir.required_fields (
Optional
[List
[str
]]) – Specify required field names which must be in the csv header line. If not, it will raise a CSVError during parsing.optional_fields (
Optional
[List
[str
]]) – Specify optional field names. If the csv file contains field names, which are not specified, it will raise a CSVError during parsing.checks (
Optional
[Dict
[str
,Callable
[[str
],bool
]]]) – Specify checks for fields as a mapping of field names to callables. The callables should return true if a given value is valid, false otherwise.do_allow_arbitrary_fields (
bool
) – If true, it will consider all possible field names as optional. Use this option with caution because it will prevent possible errors from being detected.