我有一组从第三方应用程序生成的csv文件。在每个文件的顶部,它有一个标题,空行,关于内容的X行,空白行,然后其余的是实际的csv。
由于行数是可变的,我可以跳过X行。我目前正在使用拆分并获取列数来跳过这些行,但我确信有更好的方法。
我可以用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,
答案 0 :(得分:1)
是的,你可以用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