简体   繁体   English

python-跳过CSV文件的顶行

[英]python - skip top lines of CSV file

I have a collection of csv files generated from a third party application. 我有一个从第三方应用程序生成的csv文件的集合。 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. 每个文件的顶部都有标题,空白行,关于内容的X行,空白行,然后剩下的就是实际的csv。

Since the number of lines is variable I can just skip X lines. 由于行数是可变的,所以我可以跳过X行。 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. 我目前正在通过使用split并获取列数来跳过行,但是我敢肯定有更好的方法。

can I do this with csvreader or pandas? 我可以使用csvreader或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 : 是的,您可以使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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