I have not used the csv module
in python
before, but it seems like a great tool to use for my needs.
The problem I am facing is that my csv-file
looks a bit strange. (it was hard to notice at first)
See the example below for what I mean:
A|B|C|D|x|F|G|x|H
1||2||3|4||5|6
1||2||3|4||5|6
1||2||3|4||5|6
1||2||3|4||5|6
There are some indexes (such as column-x) above which should be read as Dx and Gx. The D-column and G-column are then empty columns.
So what I need is that when csv is processing the header it should create the headers like Dx and Gx and neglect the D and G columns and take the value from the x-column instead.
(I tried to read the file as is with the following code first and post process it, but it is not possible since the column name x is always the same, and csv.DictReader
seems to discard repetitive column-names)
with open('myCSV.log') as csvFile:
reader = csv.DictReader(csvFile, dialect='pipes')
for row in reader:
print row
Can this be achieved with a dialect
, or some other fancy way of configuring the csv-module
or do I have to parse this myself?