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
liuxurong
V2EX  ›  Python

Jinja2 统计某个字符 在字符串出现次数 wordcount(s) 失效?

  •  
  •   liuxurong · 2013-01-15 18:20:18 +08:00 · 3390 次点击
    这是一个创建于 4090 天前的主题,其中的信息可能已经有所发展或是发生改变。
    http://docs.torriacg.org/docs/jinja2/templates.html
    wordcount(s)
    Count the words in that string.


    {{results.name|wordcount('a')}}
    {{results.name|wordcount(a)}}


    均不行哦,都报

    TypeError: do_wordcount() takes exactly 1 argument (2 given)
    第 1 条附言  ·  2013-01-15 19:48:58 +08:00
    那 Jinja2 如何统计某个字符 在字符串出现次数 ?
    7 条回复    1970-01-01 08:00:00 +08:00
    SErHo
        1
    SErHo  
       2013-01-15 18:28:43 +08:00   ❤️ 1
    你理解错了,这个是用来统计字符串有多少个单词组成的。
    ratazzi
        2
    ratazzi  
       2013-01-15 18:29:11 +08:00
    就不是你那样用的
    {{results.name|wordcount}}

    话说你到底取哪个的长度,| 前面是参数,wordcount 是不要带括号的
    ratazzi
        3
    ratazzi  
       2013-01-15 18:30:47 +08:00
    囧,最近在 V2EX 回复老是光看代码没仔细看文字
    liuxurong
        4
    liuxurong  
    OP
       2013-01-15 19:18:57 +08:00
    @SErHo 好吧。理解错了。
    liuxurong
        5
    liuxurong  
    OP
       2013-01-15 19:19:14 +08:00
    谢谢各位
    zhy0216
        6
    zhy0216  
       2013-01-15 19:59:06 +08:00   ❤️ 1
    你可以自己写一个函数, 把这个函数传进去
    或者自己写一个filter
    SErHo
        7
    SErHo  
       2013-01-15 22:24:58 +08:00   ❤️ 1
    可以这样{{string.count(str)}},这样可以统计 string 中 str 出现的次数。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3263 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 14:02 · PVG 22:02 · LAX 07:02 · JFK 10:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.