首页 > 解决方案 > 获取txt文件中的特定行

问题描述

[interfaces.ifTable.ifEntry.ifDescr.13],(STRING),XXX Ethernet 1Gb   
4-port 111T Adapter #3   
[interfaces.ifTable.ifEntry.ifDescr.14],(STRING) XXX Ethernet 1Gb   
4-port 111T Adapter   
[interfaces.ifTable.ifEntry.ifDescr.18],(STRING),XXX Ethernet 1Gb   
4-port 111T Adapter #4   
[interfaces.ifTable.ifEntry.ifDescr.23],(STRING),XXX Ethernet 1Gb   
4-port 111T Adapter #2   
[interfaces.ifTable.ifEntry.ifDescr.38],(STRING),XXX Ethernet 1Gb   
4-port 111T Adapter #2-WFP Native MAC Layer LightWeight Filter-0000 
[interfaces.ifTable.ifEntry.ifDescr.39],(STRING),XXX Ethernet 1Gb   
4-port 111T Adapter #2-QoS Packet Scheduler-0000   
[interfaces.ifTable.ifEntry.ifDescr.40],(STRING),XXX Ethernet 1Gb   
4-port 111T Adapter #2-WFP 802.3 MAC Layer LightWeight Filter-0000  
[interfaces.ifTable.ifEntry.ifDescr.41],(STRING),XXX Ethernet 1Gb   
4-port 111T Adapter #3-WFP Native MAC Layer LightWeight Filter-0000 
[interfaces.ifTable.ifEntry.ifDescr.42],(STRING),XXX Ethernet 1Gb   
4-port 111T Adapter #3-QoS Packet Scheduler-0000   
[interfaces.ifTable.ifEntry.ifDescr.43],(STRING),XXX Ethernet 1Gb   
4-port 111T Adapter #3-WFP 802.3 MAC Layer LightWeight Filter-0000  
[interfaces.ifTable.ifEntry.ifDescr.44],(STRING),XXX Ethernet 1Gb   
4-port 111T Adapter #4-WFP Native MAC Layer LightWeight Filter-0000 
[interfaces.ifTable.ifEntry.ifDescr.45],(STRING),XXX Ethernet 1Gb   
4-port 111T Adapter #4-QoS Packet Scheduler-0000   
[interfaces.ifTable.ifEntry.ifDescr.46],(STRING),XXX Ethernet 1Gb   
4-port 111T Adapter #4-WFP 802.3 MAC Layer LightWeight Filter-0000  
[interfaces.ifTable.ifEntry.ifDescr.47],(STRING),XXX Ethernet 1Gb   
4-port 111T Adapter-WFP Native MAC Layer LightWeight Filter-0000   
[interfaces.ifTable.ifEntry.ifDescr.48],(STRING),XXX Ethernet 1Gb   
4-port 111T Adapter-QoS Packet Scheduler-0000   
[interfaces.ifTable.ifEntry.ifDescr.49],(STRING),XXX Ethernet 1Gb   
4-port 111T Adapter-WFP 802.3 MAC Layer LightWeight Filter-0000

这是我的控制台结果,如果我只想要这样的结果:

[interfaces.ifTable.ifEntry.ifDescr.13],(STRING),XXX Ethernet 1Gb 4-port 111T Adapter #3
[interfaces.ifTable.ifEntry.ifDescr.14],(STRING) XXX Ethernet 1Gb 4-port 111T Adapter
[interfaces.ifTable.ifEntry.ifDescr.18],(STRING),XXX Ethernet 1Gb 4-port 111T Adapter #4
[interfaces.ifTable.ifEntry.ifDescr.23],(STRING),XXX Ethernet 1Gb 4-port 111T Adapter #2

我怎样才能做到这一点?谢谢。

我的代码是

import os

def check(file_name, str1):
    """ Check if any line in the file contains given string """
    # Open the file in read only mode
    with open(file_name, 'r',encoding="utf-8",errors="ignore") as read_obj:
        # Read all lines in the file one by one
        for line in read_obj:
            # For each line, check if line contains the string
            if str1 in line:
                return True
    return False
def search(file_name, str2):
    """Search for the given string in file and return lines containing that string,
    along with line numbers"""
    line_number = 0
    list_of_results = []
    # Open the file in read only mode
    with open(file_name, 'r',encoding="utf-8",errors="ignore") as read_obj:
        # Read all lines in the file one by one
        for line in read_obj:
            # For each line, check if line contains the string
            line_number += 1
            if str2 in line:
                # If yes, then add the line number & line as a tuple in the list
    # Return list of tuples containing line numbers and lines where string is found
    return list_of_results

def search_multiple(file_name, list_of_strings):
    """Get line from the file along with line numbers, which contains any string from the list"""
    line_number = 0
    list_of_results = []
    # Open the file in read only mode use encoding and erros to block the error String
    with open(file_name, 'r',encoding="utf-8",errors="ignore") as read:
        # Read all lines in the file one by one
        for line in read:
            line_number += 1
            # For each line, check if line contains any string from the list of strings
            for x in list_of_strings:
                #Filter unwanted value
                if x in line:
                    # If any string is found in line, then append that line along with line number in list
                    list_of_results.append((x,line_number,line.rstrip()))
    # Return list of tuples containing matched string, line numbers and lines where string is found
    return list_of_results

def main():
    print('*** Search for a string in file & get all lines containing the string along with line numbers ****')
    matched_lines = search('C:\mib_information\SNMPINFO\snmpinfo.txt', '366T')
    for elem in matched_lines:
        print(elem[1])

if __name__ == '__main__':
    main()

标签: python-3.x

解决方案


如果上面提到的(控制台结果)数据在文件中(比如说'file.txt'),那么试试readlines方法,比如

print(open('file.txt', 'r').readlines()[:4])

输出:

[
    '[interfaces.ifTable.ifEntry.ifDescr.13],(STRING),XXX Ethernet 1Gb 4-port 111T Adapter #3',
    '[interfaces.ifTable.ifEntry.ifDescr.14],(STRING) XXX Ethernet 1Gb 4-port 111T Adapter',
    '[interfaces.ifTable.ifEntry.ifDescr.18],(STRING),XXX Ethernet 1Gb 4-port 111T Adapter #4',
    '[interfaces.ifTable.ifEntry.ifDescr.23],(STRING),XXX Ethernet 1Gb 4-port 111T Adapter #2'
]

如果您希望它像您在问题中提到的那样显示(例如用换行符分隔),那么,

print('\n'.join(open('file.txt', 'r').readlines()[:4]))

输出:

[interfaces.ifTable.ifEntry.ifDescr.13],(STRING),XXX Ethernet 1Gb 4-port 111T Adapter #3
[interfaces.ifTable.ifEntry.ifDescr.14],(STRING) XXX Ethernet 1Gb 4-port 111T Adapter
[interfaces.ifTable.ifEntry.ifDescr.18],(STRING),XXX Ethernet 1Gb 4-port 111T Adapter #4
[interfaces.ifTable.ifEntry.ifDescr.23],(STRING),XXX Ethernet 1Gb 4-port 111T Adapter #2

告诉我它是否适合你...


推荐阅读