V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
pmispig
V2EX  ›  Python

请问怎么更改 string.format()所用的{}

  •  
  •   pmispig · 2020-05-20 15:41:54 +08:00 · 2360 次点击
    这是一个创建于 1435 天前的主题,其中的信息可能已经有所发展或是发生改变。
    可以改成 {{ }} 或者 {% %} 吗
    第 1 条附言  ·  2020-05-20 17:28:56 +08:00
    因为 shell 脚本里很多 {}了,要从 format 格式化,找了半天也没好得,jinja2 又和其他得字符冲突了,自己用正则写了一个 Template
    5 条回复    2020-05-21 11:53:02 +08:00
    ClericPy
        1
    ClericPy  
       2020-05-20 16:11:16 +08:00
    string.format() 之前看的是不支持自定义定界符的, 现在不确定

    如果不是太纠结 .format 的话, string.Template 有个 delimiter 考虑继承后改它, 或者考虑 jinja2 等其他库

    之前翻过没找到什么太好的
    rayhy
        2
    rayhy  
       2020-05-20 16:16:36 +08:00 via Android
    要不再写一个替换函数。。运行 format 之前先把{%替换成{{
    jdhao
        3
    jdhao  
       2020-05-20 16:17:36 +08:00 via Android
    先说需求,说不定有更好的方法
    xpresslink
        4
    xpresslink  
       2020-05-21 09:50:30 +08:00
    用{}本身做转义符,如果要显示{} 要写成 {{}}
    Latin
        5
    Latin  
       2020-05-21 11:53:02 +08:00
    alembic 用的是 mako 模版
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1257 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:24 · PVG 07:24 · LAX 16:24 · JFK 19:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.