python-3.x - 获取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()
解决方案
如果上面提到的(控制台结果)数据在文件中(比如说'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
告诉我它是否适合你...
推荐阅读
- c# - Umbraco Mediaservice.createmedia for Video
- html - 我希望视差效果也包括 h1,这样它就可以向上滚动而没有后面的图像,你怎么能这样做?
- python - 将 traces.plot 标签从日期更改为小时
- google-chrome - 我的 chrome 和 edge 浏览器似乎放大了我使用 html 和 css 开发的网站。在Firefox中它是完美的
- python - 如何将 EEG 信号数据拆分为训练和测试?
- linux - 为什么这个内核模块不处理打开和读取系统调用?
- module - 引用兄弟模块(例如 Diesel 生成的模式)的正确方法是什么?
- c# - 在 Xamarin.Android 中将 Json 对象项中的错误值保存和检索到 IShared 首选项(始终返回注销值)
- python - 需要帮助才能使用瑞典语字母
- nuget - Nuget 包未安装在 .Net Framework 中