python - 跳过CSV文件的顶部行 [英] python - skip top lines of CSV file

查看:436
本文介绍了python - 跳过CSV文件的顶部行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从第三方应用程序生成的csv文件的集合。在每个文件的顶部它有一个标题,空白行,关于内容的X行,空行,然后剩下的是实际的csv。



由于行数是可变的,我可以跳过X行。我现在通过使用拆分和获取列数跳过线,但我相信有一个更好的方法。



我可以使用csvreader或熊猫?

 #当前代码
for line in greport.data.splitlines():
#行使用字段
fields = line.rstrip()。rstrip(',')。split(',')

如果len(fields) 5:
continue
else:
< process file>

 #示例文件
报表标题

服务器名称:all
组名称:all
客户端名称:all
保存集名称: all
状态:all
备份类型:all
级别:all
组开始时间:从11/11/14 6:00:00 PM到11/12/14 5 :59:00 PM

客户名称,保存集名称,保存集ID,组开始时间,保存类型,级别,状态
server1,All ,, 11/11/14 6: 00:00 PM,save,skip,succeeded,
server2,All ,, 11/11/14 6:00:00,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,

是的,你可以通过实现相同的效果。 csv

  import csv 

with open 'r')as f:
reader = csv.reader(f)

对于阅读器中的行:
如果len(row) 5:
continue

#process the data


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

这篇关于python - 跳过CSV文件的顶部行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆