推荐学习书目
› 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 · 4545 views
    This topic created in 5045 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
    SErHo
    Jan 15, 2013❤️1
    1
    你理解错了,这个是用来统计字符串有多少个单词组成的。
    ratazzi
    ratazzi
    PRO
    Jan 15, 2013
    2
    就不是你那样用的
    {{results.name|wordcount}}

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