簡體   English   中英

python-跳過CSV文件的頂行

[英]python - skip top lines of CSV file

我有一個從第三方應用程序生成的csv文件的集合。 每個文件的頂部都有標題,空白行,關於內容的X行,空白行,然后剩下的就是實際的csv。

由於行數是可變的,所以我可以跳過X行。 我目前正在通過使用split並獲取列數來跳過行,但是我敢肯定有更好的方法。

我可以使用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,

是的,您可以使用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