用于(括号)的Python .replace用法和一些错误错误

时间:2015-04-19 06:13:20

标签: python sqlite

最新问题:

   iterator = p.finditer(s)
NameError: name 's' is not defined

如何解决?

在我在sqlite3数据库中选择命令后,我正在编写一个我更喜欢的函数。我可以在(括号)

中改变我的话

我已经为.replace编写了一些python,但它似乎无法在我的函数中运行。 任何人都可以检查我的错误谢谢。

这是我的功能而不添加.replace()

def readciscodevice(function, device):
            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:")
    b = input("device:")
    p = re.compile('\(.*?\)')
iterator = p.finditer(s)
for match in iterator:
    s = s[:match.start()] + [match.start():match.end()].replace(match.group(), dict[match.group()]) + s[match.end()]
        for result in readciscodevice(a,b):
                print(result[0])

现在我将.replace添加到我的函数的最后一个(存在一些错误......)

a = input("function:")
            b = input("device:")
 for match in iterator:
        s = s[:match.start()] + [match.start():match.end()].replace(match.group(), dict[match.group()]) + s[match.end()]
            for result in readciscodevice(a,b):
                    print(result[0])
            iterator = p.finditer(s)

error:`:match.end()` ---SyntaxError: invalid syntax
error:`.replace`---statement expected, found Py:RBRACKET Statement expected, found Py:DOT ,unsolved reference 'replace'

在功能输出之前:

interface fa(fanumber)/(fanumber)
ip adress (ip) (subnet mask) 

我希望将我的参数替换为(括号)的预期输出:

interface fa0/1
ip adress 192.168.2.1 255.255.255.0

任何人都可以帮我解决一些错误和替换问题吗? 谢谢

1 个答案:

答案 0 :(得分:0)

试试这个。你错过了s

s = s[:match.start()] + s[match.start():match.end()].replace(match.group(), dict[match.group()]) + s[match.end()]