csv.py: allow for multiline values
This commit is contained in:
@ -7,6 +7,8 @@ import datetime
|
|||||||
|
|
||||||
RE_INT = re.compile(r'-?[0-9]+')
|
RE_INT = re.compile(r'-?[0-9]+')
|
||||||
RE_FLOAT = re.compile(r'-?[0-9]+\.[0-9]+')
|
RE_FLOAT = re.compile(r'-?[0-9]+\.[0-9]+')
|
||||||
|
RE_STR_START = re.compile(r'.*;"[^"]*$')
|
||||||
|
RE_STR_END = re.compile(r'^[^"]*";.*')
|
||||||
|
|
||||||
|
|
||||||
def cast_value(value: str) -> Any:
|
def cast_value(value: str) -> Any:
|
||||||
@ -73,7 +75,18 @@ def parse(filename: str) -> Iterator[Tuple]:
|
|||||||
with open(filename, 'r', encoding='utf-8') as f:
|
with open(filename, 'r', encoding='utf-8') as f:
|
||||||
lines = f.__iter__()
|
lines = f.__iter__()
|
||||||
yield tuple([part.strip() for part in next(lines).split(';')])
|
yield tuple([part.strip() for part in next(lines).split(';')])
|
||||||
for line in lines:
|
in_str = False
|
||||||
|
for cur_line in lines:
|
||||||
|
if in_str:
|
||||||
|
line += cur_line
|
||||||
|
if not RE_STR_END.match(cur_line):
|
||||||
|
continue
|
||||||
|
in_str = False
|
||||||
|
else:
|
||||||
|
line = cur_line
|
||||||
|
if RE_STR_START.match(cur_line):
|
||||||
|
in_str = True
|
||||||
|
continue
|
||||||
yield tuple([cast_value(part) for part in parse_line(line)])
|
yield tuple([cast_value(part) for part in parse_line(line)])
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user