I would like to use python read and write files of the following format:
#h -F, field1 field2 field3
a,b,c
d,e,f
# some comments
g,h,i
This file closely resembles a typical CSV, except for the following:
- The header line starts with #h
- The second element of the header line is a tag to denote the delimiter
- The remaining elements of the header are field names (always separated by a single space)
- Comment lines always start with # and can be scattered throughout the file
Is there any way I can use csv.DictReader() and csv.DictWriter() to read and write these files?
You can parse the first line separately to find the delimiter and fieldnames:
Note that
csv.DictReadercan take any iterable as its first argument. So to skip the comments, you can wrapfin an iterator (skip_comments) which yields only non-comment lines:On the data you posted this yields
To write a file in this format, you could use a
headerhelper function:Note that you can write to
output.csvusingg.write(for header or comment lines) orwriter.writerow(for csv).