2005年04月21日 星期四 11:15
要实现这个操作, python也必须使用正则表达式。 Import re ... 看看这个吧: http://docs.python.org/lib/module-re.html -----邮件原件----- 发件人: python-chinese-bounces at lists.python.cn [mailto:python-chinese-bounces at lists.python.cn] 代表 cpunion 发送时间: 2005年4月21日 9:31 收件人: python-chinese at lists.python.cn 主题: Re: [python-chinese] 请问python中如何实现类似perl的变量替换功能 vars = {'name':'test'} HTML = "...%(name)s ..." out = HTML % vars_ print out Falls Huang 写道: >Hi! > 我最近从perl 转到 python, 有个这个问题: > 例如这行perl代码: $HTML =~ s/\$(\w+)/${$1}/g; > >其工作原理是这样的:$HTML是一个字符串,包含了一个 html文件的内容。正则表 >达式搜索以“$”开头的变量名,“\w”表示字符(a-z,A-Z,0-9 或下划线),“+”意 味着 >一个或多个字符。“\w+”两边的括号表示要把匹配的变量名存到“$1”中,最后, >“${$1}”计算出变量的值。 > >如 >$name="test" >$HTML="...$name ...." >执行这句后: $HTML =~ s/\$(\w+)/${$1}/g; >$HTML的值就变为了: "...test ....." > >Regards >Falls Huang > >_______________________________________________ >python-chinese list >python-chinese at lists.python.cn >http://python.cn/mailman/listinfo/python-chinese > > > > _______________________________________________ python-chinese list python-chinese at lists.python.cn http://python.cn/mailman/listinfo/python-chinese
2005年04月21日 星期四 13:24
张雄也混这儿啦?呵呵 On 4/21/05, 张雄 <zhangxiong at runway.cn.net> wrote: > 要实现这个操作, python也必须使用正则表达式。 > Import re > ... > > 看看这个吧: > http://docs.python.org/lib/module-re.html > > -----邮件原件----- > 发件人: python-chinese-bounces at lists.python.cn > [mailto:python-chinese-bounces at lists.python.cn] 代表 cpunion > 发送时间: 2005年4月21日 9:31 > 收件人: python-chinese at lists.python.cn > 主题: Re: [python-chinese] 请问python中如何实现类似perl的变量替换功能 > > vars = {'name':'test'} > HTML = "...%(name)s ..." > out = HTML % vars_ > print out > > Falls Huang 写道: > > >Hi! > > 我最近从perl 转到 python, 有个这个问题: > > 例如这行perl代码: $HTML =~ s/\$(\w+)/${$1}/g; > > > >其工作原理是这样的:$HTML是一个字符串,包含了一个 html文件的内容。正则表 > >达式搜索以"$"开头的变量名,"\w"表示字符(a-z,A-Z,0-9 或下划线),"+"意 > 味着 > >一个或多个字符。"\w+"两边的括号表示要把匹配的变量名存到"$1"中,最后, > >"${$1}"计算出变量的值。 > > > >如 > >$name="test" > >$HTML="...$name ...." > >执行这句后: $HTML =~ s/\$(\w+)/${$1}/g; > >$HTML的值就变为了: "...test ....." > > > >Regards > >Falls Huang > > > >_______________________________________________ > >python-chinese list > >python-chinese at lists.python.cn > >http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > > >
2005年04月21日 星期四 20:41
a = {'name' : 'test'}
html = '...$name ...'
b = re.match(r"^.*\$(\w+).*$", html).group(1)
c = string.replace(html, '$%s' % b, a[b])
没有perl里面写得那么简单,但是代码很容易看懂!
----- Original Message -----
From: "Feng XueHu" <samhoo at gmail.com>
To: <python-chinese at lists.python.cn>
Sent: Thursday, April 21, 2005 1:24 PM
Subject: Re: 答复: [python-chinese] 请问python中如何实现 类似perl的变量替换
功能
> 张雄也混这儿啦?呵呵
>
> On 4/21/05, 张雄 <zhangxiong at runway.cn.net> wrote:
> > 要实现这个操作, python也必须使用正则表达式。
> > Import re
> > ...
> >
> > 看看这个吧:
> > http://docs.python.org/lib/module-re.html
> >
> > -----邮件原件-----
> > 发件人: python-chinese-bounces at lists.python.cn
> > [mailto:python-chinese-bounces at lists.python.cn] 代表 cpunion
> > 发送时间: 2005年4月21日 9:31
> > 收件人: python-chinese at lists.python.cn
> > 主题: Re: [python-chinese] 请问python中如何实现类似perl的变量替换功能
> >
> > vars = {'name':'test'}
> > HTML = "...%(name)s ..."
> > out = HTML % vars_
> > print out
> >
> > Falls Huang 写道:
> >
> > >Hi!
> > > 我最近从perl 转到 python, 有个这个问题:
> > > 例如这行perl代码: $HTML =~ s/\$(\w+)/${$1}/g;
> > >
> > >其工作原理是这样的:$HTML是一个字符串,包含了一个 html文件的内容。正则
表
> > >达式搜索以"$"开头的变量名,"\w"表示字符(a-z,A-Z,0-9 或下划线),"+"意
> > 味着
> > >一个或多个字符。"\w+"两边的括号表示要把匹配的变量名存到"$1"中,最后,
> > >"${$1}"计算出变量的值。
> > >
> > >如
> > >$name="test"
> > >$HTML="...$name ...."
> > >执行这句后: $HTML =~ s/\$(\w+)/${$1}/g;
> > >$HTML的值就变为了: "...test ....."
> > >
> > >Regards
> > >Falls Huang
> > >
> > >_______________________________________________
> > >python-chinese list
> > >python-chinese at lists.python.cn
> > >http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> > >
> > >
> > _______________________________________________
> > python-chinese list
> > python-chinese at lists.python.cn
> > http://python.cn/mailman/listinfo/python-chinese
> >
> >
> > _______________________________________________
> > python-chinese list
> > python-chinese at lists.python.cn
> > http://python.cn/mailman/listinfo/python-chinese
> >
> >
> >
>
----------------------------------------------------------------------------
----
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
>
2005年04月21日 星期四 21:20
非常感谢大家的帮忙,我在 PyPi(http://www.python.org/pypi?%3Aaction=home) 找到了我需要的模块:HTMLTemplate 1.2.1 pypi 似乎打算建成一个类似perl的cpan 站点, 很方便。 Chun Lin Zhang wrote: > a = {'name' : 'test'} > html = '...$name ...' > b = re.match(r"^.*\$(\w+).*$", html).group(1) > c = string.replace(html, '$%s' % b, a[b]) > > 没有perl里面写得那么简单,但是代码很容易看懂! >
Zeuux © 2025
京ICP备05028076号