代码之家  ›  专栏  ›  技术社区  ›  Kevin Müller

更改列表Python Flask中的字符

  •  1
  • Kevin Müller  · 技术社区  · 7 年前

    @app.route("/[<string:pfade>]")
    def testaufruf(pfade):
    
       s=list(pfade)
    
       part = [i for i,x in enumerate(s) if x=="$"]
    
       print(part)
    
       s[part]="\\"
    
       print(part)
    

    我的问题是我想通过 127.0.0.1:5000/[Test$path1]

    Test$path1 将其列为列表并希望替换 $ 用一个 \

    这些行工作:

    s=list(pfade)
    
    part = [i for i,x in enumerate(s) if x=="$"]
    
    print(part)
    

    $ $ 不起作用。我确实找了很多,但找不到解决这个问题的办法。

    3 回复  |  直到 7 年前
        1
  •  2
  •   Alex Taylor    7 年前

    字符串有 replace

    part = pfade.replace('$', '\\')
    

    注意 \

        2
  •  1
  •   Mousa Halaseh    7 年前

    这一行将返回一个整数列表

    part = [i for i,x in enumerate(s) if x=="$"]
    

    你只是想索引 s 有了列表,您将面临以下错误

    TypeError: list indices must be integers or slices, not list.
    

    要解决这个问题:

    parts = [i for i,x in enumerate(s) if x=="$"]
    for part in parts:
        s[part] = '\\'
    
    print(s)
    
        3
  •  0
  •   Kevin Müller    7 年前

    而不是使用 part = [i for i,x in enumerate(s) if x=="$"]

    part = s.index("$", 0)

    s[part] = "\\" 替换 $ 具有 "\"

    推荐文章