I need to parse the first column of a document into a list ['item1', 'item2', ...]
, and this document can be:
- a TXT document (items separated by newlines)
- a CSV document with one single column (then it's similar to TXT document)
- a CSV with many columns, separated by
;
- a CSV with many columns, separated by
,
- a XLS with one or many columns
- a XLSX with one or many columns
I was about to code it with many cases:
ext = os.path.splitext(f)[1].lower()
if ext == '.txt':
with open(f, 'r') as f:
L = f.read().splitlines()
if ext == '.csv':
reader = csv.reader(...)
...
if ext == '.xls':
...
but is there a general higher-level tool in Python that does all of this directly?