csvutil
This library is considered production ready.
Utility functions for CSV file loading and conversion.
syntax:
csvutil.to_dicts(data, fields, on_invalid=None)
Parsing a CSV file, each line of a CSV file corresponds to a dict.
xxx.csv
123.5,xp,28
10,qq,30
to_dicts(d,[('ts', int), ('name', lambda s: s.upper()), 'age'])
output:
[
{'ts': 123, 'name': 'XP', 'age': '28'},
{'ts': 10, 'name': 'QQ', 'age': '30'}
]
arguments
-
data
: Same as the first argument of CSV.DictReader.data = open('xxx.csv')
-
fields
: Specify the name and type of each field in each row of data in CSV. -
on_invalid
: on_invalid(idx, field, val, exception):Callback function when parsing error.- idx: col number.
- field: Is the field definition for idx passed in by to_dicts, for example ("name", to_upper).
- val: The value of this field.
- exception: Object that throws an exception.
-
on_invalid
: If the value is None, an exception is thrown directly; if the value is 'ignore', it is ignored directly.
return: Returns an iterable, where each element is a dict, and each dict corresponds to a row in CSV.
Cheng Yang (程洋) [email protected]
The MIT License (MIT)
Copyright (c) 2018 Cheng Yang(程洋) [email protected]