【问题标题】:need help to slice the words in python需要帮助在 python 中切片单词
【发布时间】:2017-06-13 04:27:47
【问题描述】:

需要帮助从下面的输出中找到字符串 NAME: "3640 chassis"SN: FF1045C5

术语长度 0

R1#sh 库存

名称:“3640 机箱”,DESCR:“3640 机箱”

PID: , VID: 0xFF, SN: FF1045C5

名称:“单端口 Fastethernet TX”,DESCR:“单端口 Fastethernet TX”

PID:NM-1FE-TX=,VID:1.0,SN:7720321

名称:“单端口 Fastethernet TX”,DESCR:“单端口 Fastethernet TX”

PID:NM-1FE-TX=,VID:1.0,SN:7720321

名称:“单端口 Fastethernet TX”,DESCR:“单端口 Fastethernet TX”

PID:NM-1FE-TX=,VID:1.0,SN:7720321

名称:“单端口 Fastethernet TX”,DESCR:“单端口 Fastethernet TX”

PID:NM-1FE-TX=,VID:1.0,SN:7720321

R1#

问候 德夫拉吉

【问题讨论】:

  • 你没有问问题。
  • 请编辑您的问题,因为它目前太不清楚关于您要询问的内容
  • 我尝试在下面获得所需的输出,但没有运气。 input_data = open('abc.txt') out = input_data.find('NAME:') 打印输出 output = input_data.find('SN:') 打印输出 print input_data[out:].strip("\t\n \r\n") print input_data[output:].strip("\t\n\r\n")

标签: python network-programming


【解决方案1】:

所以,这是输入文件 test.txt

term len 0

R1#sh inventory

NAME: "3640 chassis", DESCR: "3640 chassis" 
PID: , VID: 0xFF, SN: FF1045C5

NAME: "One port Fastethernet TX", DESCR: "One port Fastethernet TX" 
PID: NM-1FE-TX= , VID: 1.0, SN: 7720321

NAME: "One port Fastethernet TX", DESCR: "One port Fastethernet TX" 
PID: NM-1FE-TX= , VID: 1.0, SN: 7720321

NAME: "One port Fastethernet TX", DESCR: "One port Fastethernet TX" 
PID: NM-1FE-TX= , VID: 1.0, SN: 7720321

NAME: "One port Fastethernet TX", DESCR: "One port Fastethernet TX" 
PID: NM-1FE-TX= , VID: 1.0, SN: 7720321

R1#

这可能会有所帮助:

with open("test.txt") as f:
    for eachLine in f:
        if 'NAME: "3640 chassis"' in eachLine or 'SN: FF1045C5' in eachLine:                
            print "match found in line: ", eachLine

输出如下:

match found in line:  NAME: "3640 chassis", DESCR: "3640 chassis" 
match found in line:  PID: , VID: 0xFF, SN: FF1045C5

【讨论】:

  • 感谢 tkhurana 的回复。但是对于给定的脚本,我什么也没得到,因为 'NAME: "3640 chassis"' 和 'SN: FF1045C5' 在不同的行中。
  • @Devraj 根据 'NAME: "3640 chassis"' 和 'SN: FF1045C5' 在不同行的情况更新了我的答案
  • 谢谢你。您的代码帮助我确定所需的输出。
  • @Devraj 很高兴它对您有所帮助,如果您觉得它有用,请不要犹豫接受我的回答 :)
猜你喜欢
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
  • 2010-11-10
  • 2018-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多