简体   繁体   中英

python - skip top lines of CSV file

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:
       <process file>

#

# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM