简体   繁体   English

有没有办法跳过熊猫中的行,直到 csv 说“飞行表”?

[英]is there any way to skip rows in pandas until csv says "Flight Table"?

N11682,aircraft,C172,,Cessna,C172SP,airplane,airplane_single_engine_land,fixed_tricycle,Piston,false,false,false,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Flights Table,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Date,AircraftID,From,To,Route,TimeOut,TimeOff,TimeOn,TimeIn,OnDuty,OffDuty,TotalTime,PIC,SIC,Night,Solo,CrossCountry,Distance,DayTakeoffs,DayLandingsFullStop,NightTakeoffs,NightLandingsFullStop,AllLandings,ActualInstrument,SimulatedInstrument,HobbsStart,HobbsEnd,TachStart,TachEnd,Holds,Approach1,Approach2,Approach3,Approach4,Approach5,Approach6,DualGiven,DualReceived,SimulatedFlight,GroundTraining,InstructorName,InstructorComments,Person1,Person2,Person3,Person4,Person5,Person6,FlightReview,Checkride,IPC,PilotComments
2020-11-01,N172TG,KSFM,KSFM,,,,,,,,0.8,0.8,0.0,0.0,0.0,0.0,0.00,0,0,0,0,0,0.0,0.0,0.00,0.00,0.00,0.00,0,,,,,,,0.8,0.0,0.0,0.0,,,,,,,,,false,false,false
2020-11-01,N916BA,KSFM,KSFM,,,,,,,,0.5,0.5,0.0,0.0,0.0,0.0,0.00,0,0,0,0,0,0.0,0.0,0.00,0.00,0.00,0.00,0,,,,,,,0.5,0.0,0.0,0.0,,,,,,,,,false,false,false31,N172TG,KSFM,KSFM,KASH,,,,,,,1.7,1.7,0.0,0.0,0.0,1.7,0.00,0,0,0,0,0,0.0,0.0,0.00,0.00,0.00,0.00,0,,,,,,,1.7,0.0,0.0,0.0,,,,,,,,,false,false,false

Heres my CSV, although I want it to skip multiple rows prior to the string "Flight Table" is this possible?这是我的 CSV,虽然我希望它在字符串“飞行表”之前跳过多行,这可能吗? Ive used skiprows but I need a little more versatility我用过skiprows,但我需要更多的多功能性

I would:我会:

  1. Open the CSV file打开 CSV 文件
  2. loop through it as an iterator作为迭代器循环遍历它
  3. if a line starts with your magic string, pass the remaining lines to pandas.read_csv如果一行以您的魔法字符串开头,请将其余行传递给pandas.read_csv
with open(mycsv, 'r') as fobj:
    for line in fobj:
        if line.startswith('Flights Table'):
            df = pandas.read_csv(fobj)

Use this link count lines manually and use skiprows.手动使用此链接计数行并使用skiprows。

If lines keep changing - use readline till you find your string and count lines programmatically and feed to skiprows如果行不断变化 - 使用 readline 直到你找到你的字符串并以编程方式计算行数并提供给 skiprows

skiprows : list-like, int or callable, optional Line numbers to skip (0-indexed) or number of lines to skip (int) at the start of the file. skiprows : list-like, int 或 callable, 可选的要跳过的行号(0-indexed)或要跳过的行数(int)在文件的开头。

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

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