I have a csv file received in a bad format (do not have control on the app that generates this CSV)
the headers of CSV and the first line are like the following:
"Start Time"
"End Time"
"Service"
"255/06:06:54","255/06:54:42","S2 AVAIL"
This is the code i use to read the csv:
import csv
import os
import sys
rootPath = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..'))
inputFile = open(rootPath + '\\input\\' + sys.argv[1], 'rt')
sys.path.append(rootPath + '\\common')
for row in csv.reader(inputFile, dialect='excel'):
if row:
print(row)
This is the output I receive:
['"Start Time"']
['End Time']
['Service']
['255/06:06:54', '255/06:54:42', 'S2 AVAIL']
The first problem is that strange character (may an encoding option missing?) also the header is wrong and cannot use DictReader on that format, which for the edit I have to do with the CSV are useful.
I could re-write a new CSV with the header correctly formatted, that is not a problem, but I do not know how to skip the first 3 lines of the CSV!? Or can I read it with the format CSV is coming?
This is the output I wish to obtain with csv.reader:
['Start Time', 'End Time', 'Service']
['255/06:06:54', '255/06:54:42', 'S2 AVAIL']
or with csv.DictReader:
OrderedDict([('Start Time', '255/06:06:54'), ('End Time', '255/06:54:42'), ('Service', 'S2 AVAIL')])