简体   繁体   English

打印出以两个不同的字符串输出开头的行?

[英]Print out lines that begin with two different string outputs?

I am trying to scan an input file and print out parts of lines that begin with a certain string. 我正在尝试扫描输入文件并打印出以特定字符串开头的行的一部分。 The text file is 10000+ lines, but I am only concerned with the beginning line, and more specifically the data within that line. 文本文件是10000+行,但是我只关心起始行,更具体地说,是该行中的数据。 For clarification, here are two lines of code which explain what I am trying to say. 为了澄清起见,以下两行代码解释了我要说的内容。

inst "N69" "IOB",placed BIOB_X11Y0 R8  ,

inst "n0975" "SLICEX",placed CLEXL_X20Y5 SLICE_X32Y5  ,

Here is the code that I have gotten to so far: 这是到目前为止我得到的代码:

searchfile = open("C:\PATH\TO\FILE.txt","r")
for line in searchfile:
    if "inst " in line: 
        print line
searchfile.close()

Now this is great if I am looking for all lines that start with "inst", but I am specifically looking for lines that start with "inst "N"" or "inst "n"". 现在,如果我要查找以“ inst”开头的所有行,那就太好了,但是我特别要寻找以“ inst” N”或“ inst” n”开头的行。 From there, I wanted to extract just the string starting with N or n. 从那里,我只想提取以N或n开头的字符串。

My idea was to first extract those lines (as shown above) to a new .txt file, then run another script to get only the portions of the lines that have N or n. 我的想法是首先将这些行(如上所示)提取到新的.txt文件中,然后运行另一个脚本以仅获取具有N或n的行部分。 In the example above, I am only concerned with N69 and n0975. 在上面的示例中,我仅关注N69和n0975。 Is there an easier method of doing this? 有更简单的方法吗?

With re.search() function: 使用re.search()函数:

Sample file.txt content: 示例file.txt内容:

inst "N69" "IOB",placed BIOB_X11Y0 R8  ,
some text
inst "n0975" "SLICEX",placed CLEXL_X20Y5 SLICE_X32Y5  ,
text
another text

import re

with open('file.txt', 'r') as f:
    for l in f.read().splitlines():
        m = re.search(r'^inst "([Nn][^"]+)"', l)
        if m:
            print(m.group(1))

The output: 输出:

N69
n0975

Yes with the re module. 是的,关于re模块。

re.finditer(r'^inst\s+\"n(\d+)\"', the_whole_file, re.I)

Will return you an iterator of all the matches. 将返回所有匹配项的迭代器。 For each match you will need to do .group(1) to get those numbers you wanted. 对于每个匹配项,您都需要执行.group(1)以获得所需的编号。

Notice that you don't need to filter the file first using this method. 请注意,您无需首先使用此方法过滤文件。 You can do this for the whole file. 您可以对整个文件执行此操作。

The output in your case will be: 在您的情况下,输出将是:

69
0975

Here is one solution: 这是一种解决方案:

with open('nfile.txt','r') as f:
    for line in f:
        if line.startswith('inst "n') or line.startswith('inst "N'):
            print line.split()[1]

For each line in the file startswith part checks if the line starts with one of your target patters. 对于文件中的每一行startswith部分检查是否符合你的目标patters中的一个开始。 If yes, it splits the line using split and prints the second component which is the part with n or N . 如果是,它将使用split分割线并打印第二个部分,该部分是具有nN的部分。

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

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