You can parse the first line separately to find the delimiter and fieldnames:
firstline = next(f).split()
delimiter = firstline[1][-1]
fields = firstline[2:]
Note that csv.DictReader
can take any iterable as its first argument. So to skip the comments, you can wrap f
in an iterator (skip_comments
) which yields only non-comment lines:
import csv
def skip_comments(iterable):
for line in iterable:
if not line.startswith('#'):
yield line
with open('data.csv','rb') as f:
firstline = next(f).split()
delimiter = firstline[1][-1]
fields = firstline[2:]
for line in csv.DictReader(skip_comments(f),
delimiter = delimiter, fieldnames = fields):
print line
On the data you posted this yields
{'field2': 'b', 'field3': 'c', 'field1': 'a'}
{'field2': 'e', 'field3': 'f', 'field1': 'd'}
{'field2': 'h', 'field3': 'i', 'field1': 'g'}
To write a file in this format, you could use a header
helper function:
def header(delimiter,fields):
return '#h -F{d} {f}\n'.format(d = delimiter, f=' '.join(fields))
with open('data.csv', 'rb') as f:
with open('output.csv', 'wb') as g:
firstline = next(f).split()
delimiter = firstline[1][-1]
fields = firstline[2:]
writer = csv.DictWriter(g, delimiter = delimiter, fieldnames = fields)
g.write(header(delimiter,fields))
for row in csv.DictReader(skip_comments(f),
delimiter = delimiter, fieldnames = fields):
writer.writerow(row)
g.write('# comment\n')
Note that you can write to output.csv
using g.write
(for header or comment lines) or writer.writerow
(for csv).