I have written a function to printout output and some of the output has a (parentheses) that I preferred to re-intput some string into the (parentheses). Now I am trying to use python and regular expression .replace and finditer. But it seems not my cup of tea. So thank you for your help.
This is my output in my database:
interface fa(1)/(2)
ip address (1) (2)
this is my expected result after adding regular expression:
interface fa0/1
ip adress 192.168.1.1 255.255.255.0
This is my function
def readciscodevice(function, device)://here are some sqlite3 statement
conn = sqlite3.connect('server.db')
cur = conn.cursor()
if device == "switch":
cur.execute(
"SELECT DISTINCT command FROM switch WHERE function =? or function='configure terminal' or function='enable' ORDER BY key ASC",
(function,))
read = cur.fetchall()
return read
elif device == "router":
cur.execute(
"SELECT DISTINCT command FROM router WHERE function =? or function='configure terminal' or function='enable' ORDER BY key ASC",
(function,))
read = cur.fetchall()
return read;
elif device == "showcommand":
cur.execute(
"SELECT DISTINCT command FROM showcommand WHERE function =? or function='enable' ORDER BY key ASC",
(function,))
read = cur.fetchall()
return read;
a = input("function:") //First, I input function field from database
b = input("device:") //I need to input the name of my elif =="name"
p = re.compile('\(.*?\)') //I am not sure what it is doing...
s = "1"
iterator = p.finditer(s) //finditer is suitable to replace parentheses?
for match in iterator:
s = s[:match.start()] + s[match.start():match.end()].replace(match.group(), dict[match.group()]) + s[match.end()]
for result in readciscodevice(a,b):
print(result[0])
Aucun commentaire:
Enregistrer un commentaire