I have a collection of csv files generated from a third party application. At the top of each file it has a title, blank line, X lines about the content, blank line, and then the rest is the actual csv.
Since the number of lines is variable I can just skip X lines. I am currently skipping the lines by using a split and getting the number of columns, but I'm sure there is a better way.
can I do this with csvreader or pandas?
# current code
for line in greport.data.splitlines():
# split up the line to work with the fields
fields = line.rstrip().rstrip(',').split(',')
if len(fields) < 5:
continue
else:
#
# sample file
Title of report
Server Name: all
Group Name: all
Client Name: all
Save Set Name: all
Status: all
Backup Type: all
Level: all
Group Start Time: from 11/11/14 6:00:00 PM to 11/12/14 5:59:00 PM
Client Name,Save Set Name,Save Set ID,Group Start Time,Save Type,Level,Status
server1,All,,11/11/14 6:00:00 PM,save,skip,succeeded,
server2,All,,11/11/14 6:00:00 PM,save,skip,succeeded,
server3,All,,11/12/14 12:00:00 AM,save,skip,succeeded,
server4,ASR:\,3630378478,11/11/14 11:00:00 PM,save,1,succeeded,
解决方案
Yes, you can achieve the same effect with csv:
import csv
with open('data', 'r') as f:
reader = csv.reader(f)
for row in reader:
if len(row) < 5:
continue
#process the data