[英]Search for a string that contains numbers python
So I have the following code: 所以我有以下代码:
cus_str = "show configuration " #intiate router command string variable
with open(var) as config_file: #open file
for line in config_file: #line by line reading of file
if '"xe-" + #anynumber + "/" #anynumber + "/" + #anynumber' in line:
line = line.replace('\n' , '').replace('"' , '').replace(']' , '') #removes all extra characters
i = "| except " + (line.split("cust ", 1)[1]) + " " #split the line and save last index (cust name)
cus_str+=i #append to string
config_file.close()
The line: if '"xe-" + #anynumber + "/" #anynumber + "/" + #anynumber' in line: Is what I'm struggling with syntax-wise. 这行代码: 如果''xe-'+ #anynumber +“ /” #anynumber +“ /” + #anynumber'这行:这是我在语法方面的努力。
I am looking to see if a line in the file contains the following string: "xe- number / number / number " (ex: "xe-6/1/10"). 我正在查看文件中的一行是否包含以下字符串:“ xe- 数字 / 数字 / 数字 ”(例如:“ xe-6 / 1/10”)。 It will always have this format, but the numbers will change. 它将始终采用这种格式,但是数字会更改。 What sort of syntax would I use to do this most efficiently. 我将使用哪种语法最有效地执行此操作。
Thanks! 谢谢!
You can use regular expressions for this. 您可以为此使用正则表达式。 Regular expressions allow you to specify a pattern of text (though not all patterns can be expressed as a regular expression). 正则表达式允许您指定文本模式(尽管并非所有模式都可以表示为正则表达式)。 We can then compile
that expression into a Pattern
object, and use that object to search
strings for the pattern. 然后,我们可以将该表达式compile
为Pattern
对象,并使用该对象在字符串中search
模式。
import re
pattern = re.compile(r'"xe-\d+/\d+/\d+"') # \d+ is "one or more digits".
# Everything else is literal
with open(var) as config_file:
for line in config_file:
if pattern.search(line): # will return a Match object if found, else None
...
Sounds like a job for the Regular Expressions library! 听起来像是正则表达式库的工作!
Is this number a date? 这个数字是日期吗? You can restrict the amount of digits. 您可以限制位数。
from re import search, compile #Import the re library with search
pattern = compile(r"xe-\d{1,2}\/\d{1,2}\/\d{1,2}") #Use this pattern to find lines
cus_str = "show configuration " #intiate router command string variable
with open(var) as config_file: #open file
for line in config_file: #line by line reading of file
if search(pattern, line): #Returns None if match is not found
line = line.replace('\n' , '').replace('"' , '').replace(']' , '') #removes all extra characters
i = "| except " + (line.split("cust ", 1)[1]) + " " #split the line and save last index (cust name)
cus_str+=i #append to string
The regular expression: xe-\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}
matches "xe-" followed by 3 groups of digit pairs with a slash separator. 正则表达式: xe-\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}
匹配“ xe-”,后跟带有斜杠分隔符的3组数字对。 Each group of digits can be 1 or 2 characters in length. 每组数字的长度可以是1个或2个字符。
You don't need to close config_file
since the with
statement does that for you when you exit the block. 您不需要关闭config_file
因为在退出该块时, with
语句会为您完成该操作。
I'm not sure what you were trying to accomplish with cus_str += i
when there is no match for the pattern. 当模式不匹配时,我不确定您要使用cus_str += i
什么。 As it stands, it will just repeat the same i
from the previous line, unless you indent that line by 1 level. 就目前而言,除非您以1级缩进该行,否则它只会重复与上一行相同的i
。 Or give you an error if the first line doesn't contain the pattern. 如果第一行不包含模式,请给您一个错误。
You can test a regex with a simple program like with some online tools like https://pythex.org/ , and then make your program, a little sample to point you in the right direction would be: 您可以使用简单的程序(例如一些在线工具,例如https://pythex.org/)测试正则表达式,然后制作程序,向您指出正确方向的一个小例子是:
import re
config_file = ["xe-6/1/10", "xf-5/5/542", "xe-4/53/32" ]
rex = re.compile(r'xe-[\d]+/[\d]+/[\d]+')
for line in config_file: #line by line reading of file
if rex.search(line):
print(line)
xe-6/1/10
xe-4/53/32
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.