Ignoring lines in python when splitting

时间:2017-06-19 14:03:57

标签: python split

So I've never worked with Python before and I'm trying to parse a text file with content such as this (but then with about 1 million lines):

PuckTaken: (1;0;226;514)
PuckTaken: (4;0;254;529)
PuckTaken: (13;1;207;460)
PuckTaken: (27;1;184;518)
PuckTaken: (50;1;180;562)
PuckTaken: (123;1;225;608)
PuckTaken: (129;1;134;571)
PuckTaken: (150;0;219;629)
EggHatched: (211; 214.709; 442.891); [[0, 0, 0.005, 136], [0, 0, 0.005, 185], [0, 0, 0.005, 108], [0, 0, 0.005, 188], [0, 0, 0.005, 121], [0, 0, 0.005, 142], [0, 0, 0.005, 102], [0, 0, 0.005, 147], [0, 0, 0.005, 104], [0, 0, 0.005, 100], [0, 0, 0.005, 163], [0, 0, 0.005, 162], [0, 0, 0.005, 111], [0, 1, 1.005, 179]]; Winner: 100
EggHatched: (290; 282.386; 580.121); [[0, 0, 0.005, 134], [0, 0, 0.005, 143], [0, 0, 0.005, 139], [0, 0, 0.005, 103], [0, 0, 0.005, 155], [0, 0, 0.005, 160], [0, 0, 0.005, 125], [0, 0, 0.005, 112], [0, 0, 0.005, 133], [0, 0, 0.005, 117], [0, 0, 0.005, 156], [0, 0, 0.005, 166], [0, 0, 0.005, 137], [0, 0, 0.005, 171], [1, 0, 1.005, 129]]; Winner: 129
EggHatched: (299; 202.399; 578.328); [[0, 0, 0.005, 106], [0, 0, 0.005, 118], [0, 0, 0.005, 181], [0, 0, 0.005, 114], [0, 0, 0.005, 186], [0, 0, 0.005, 120], [0, 0, 0.005, 129], [0, 0, 0.005, 140], [0, 1, 1.005, 175]]; Winner: 175
EggHatched: (333; 295.22; 420.14); [[0, 0, 0.005, 102], [0, 0, 0.005, 191], [0, 0, 0.005, 126], [0, 0, 0.005, 180], [0, 0, 0.005, 176], [0, 0, 0.005, 168], [0, 0, 0.005, 151]]; Winner: 180
EggHatched: (340; 213.756; 504.939); [[0, 0, 0.005, 170], [0, 0, 0.005, 135], [0, 0, 0.005, 124], [0, 0, 0.005, 169], [0, 0, 0.005, 164], [0, 0, 0.005, 172], [0, 0, 0.005, 195], [0, 0, 0.005, 184], [0, 0, 0.005, 190], [0, 0, 0.005, 197], [0, 0, 0.005, 187], [0, 0, 0.005, 107], [1, 0, 2.005, 159], [0, 1, 2.005, 130]]; Winner: 195
PuckTaken: (346;0;423;813)
PuckTaken: (358;0;446;806)
EggHatched: (385; 176.75; 544.473); [[0, 0, 0.005, 114], [0, 0, 0.005, 153], [0, 1, 1.005, 165]]; Winner: 165
PuckTaken: (401;0;497;855)

What I'm trying to get is just the first value of "EggHatched" and the last value in each bracket. These values then have to end up on 1 line in a new text file. For example for the first EggHatched line in the example I would get the following line as output: 211 136 185 108 188 121 142 102 147 104 100 163 162 111 179

Now I know that the code below will split on : and give me the value on the left of it (PuckTaken or Egghatched here for each line), but I need some way to make python ignore the "PuckTaken" lines.

for line in inputfile:
   test1 = line.split(":")[0]
   print test1

I feel like there is some easy way to do this (some python command) but Google didn't help me out unfortunately. If anyone could point me in the right direction or give me the command I'd need that would be awesome!

How do I make python ignore all lines that start with "PuckTaken"?

1 个答案:

答案 0 :(得分:0)

只需使用str.startwith()

for line in inputfile:
    if line.startwith('PuckTaken'):
        continue
    # do something with line