我想在比赛前后打印线

时间:2014-03-12 19:47:30

标签: python parsing

我想创建一个.py文件,可以打开.txt文件并读入数据,将其拆分成文字,然后我希望能够搜索日期和时间。时间可能会有所不同,可能不准确,因此它可能介于两次之间。我想打印搜索日期/时间之前和之后的两行。

到目前为止我正在使用它:

temp_list =[]
open_file = open(temp_data, "r")
contents = open_file.readlines()[57:] #skipping first 57 lines.
for i in range(len(contents)):
words = contents[i].split()
if (len(words) == 43) and (words[0] == date) and (words[1] < time): #using 43 will read/print only lines that have 43 words in them as they are the lines with important data.
    print words[0], "|", words[1], "|", words[12],  "F"

我对编码完全陌生并且到处寻找,但仍然无法找到任何东西。感谢任何帮助,谢谢!

编辑(它不允许我出于某种原因发布txt文件的图片而没有声誉):

  

2010-10-02 19:00:01.15 M~D 0 2 1 1 2 1 -38.4 22.7 3.7 NN 20 -1.1 29.2 0 -99.9 0 24.1 16.0 3 71 0 000 172 0284 0473 0949 1023 0224 0228 0.0 080 13415 3 0710~0 0 FM

     

2010-10-02 19:30:00.47 M~D 0 2 1 1 2 1 -40.2 21.7 2.3 NN 21 -1.6 29.5 0 -99.9 0 24.1 22.0 3 71 0 000 172 0301 0470 0949 1023 0191 0201 0.0 070 13416 3 0444~0 0 FM

     

2010-10-02 20:00:01.73 M~D 0 2 1 1 2 0 -28.6 20.6 2.1 NN 22 -1.8 29.1 0 -99.9 0 24.1 23.9 3 74 0 000 172 0241 0474 0949 1023 0187 0193 0.0 067 13417 1 0003~0 0 FM

1 个答案:

答案 0 :(得分:0)

如果该行匹配,您需要一个返回True的函数。看起来你的if声明正在这样做。如果返回True,则打印行contents[i-2:i+2]