推荐学习书目
› 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 · Jan 15, 2013 · 4469 views
    This topic created in 5018 days ago, the information mentioned may be changed or developed.
    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)
    Supplement 1  ·  Jan 15, 2013
    那 Jinja2 如何统计某个字符 在字符串出现次数 ?
    7 replies  •  1970-01-01 08:00:00 +08:00
    SErHo
        1
    SErHo  
       Jan 15, 2013   ❤️ 1
    你理解错了,这个是用来统计字符串有多少个单词组成的。
    ratazzi
        2
    ratazzi  
    PRO
       Jan 15, 2013
    就不是你那样用的
    {{results.name|wordcount}}

    话说你到底取哪个的长度,| 前面是参数,wordcount 是不要带括号的
    ratazzi
        3
    ratazzi  
    PRO
       Jan 15, 2013
    囧,最近在 V2EX 回复老是光看代码没仔细看文字
    liuxurong
        4
    liuxurong  
    OP
       Jan 15, 2013
    @SErHo 好吧。理解错了。
    liuxurong
        5
    liuxurong  
    OP
       Jan 15, 2013
    谢谢各位
    zhy0216
        6
    zhy0216  
       Jan 15, 2013   ❤️ 1
    你可以自己写一个函数, 把这个函数传进去
    或者自己写一个filter
    SErHo
        7
    SErHo  
       Jan 15, 2013   ❤️ 1
    可以这样{{string.count(str)}},这样可以统计 string 中 str 出现的次数。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   6137 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 06:32 · PVG 14:32 · LAX 23:32 · JFK 02:32
    ♥ Do have faith in what you're doing.