简体   繁体   English

我想在比赛前后打印该行

[英]I want to print the line before and after match

I am wanting to create a .py file that can open a .txt document and read in the data, split it into words, then I would like to be able to search for a date and time. 我想要创建一个.py文件,该文件可以打开.txt文档并读取数据,将其拆分为单词,然后希望能够搜索日期和时间。 The time can vary and may not be exact so it could fall in between two times. 时间可能会有所不同,并且可能不准确,因此可能介于两次之间。 I would like to print the two lines that fall before and after the searched date/time. 我想打印搜索日期/时间前后的两行。

I am using this so far: 到目前为止,我正在使用:

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"

I am completely new to coding and have looked everywhere but am still not able to find anything. 我是编码的新手,到处都看过,但仍然找不到任何东西。 Any help is appreciated, thank you! 任何帮助表示赞赏,谢谢!

EDIT(it will not allow me to post pictures of txt file for some reason without reputation): 编辑(由于某些原因,我无法发布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: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调频

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 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调频

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 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调频

You need a function that will return True if the line is a match. 您需要一个函数,如果该行是匹配的,它将返回True Looks like your if statement is doing that. 看起来您的if语句正在执行此操作。 If it returns True, then print the lines contents[i-2:i+2] . 如果返回True,则打印行contents[i-2:i+2]

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

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