更好的方法:myPage + ='more html',... [英] better way than: myPage += 'more html' , ...

查看:65
本文介绍了更好的方法:myPage + ='more html',...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个python cgi程序,它使用print语句编写html。

程序已经增长,并且由于我赢了的原因我不知道,我需要

用字符串构建页面并打印它立刻。


我记得在某个地方读过连续构建字符串

" + ="声明是低效的,但我不知道任何其他选择,并且我的搜索尝试空了。有没有比这更好,更有效的方式




#--------------- ------------------

htmlPage ="< html>< header>< / header>< body>"

htmlPage + =" more html"

....

htmlPage + ="更多html"

....

htmlPage + ="< / body>< / html>"

print htmlPage

# -------------------------------------


谢谢,Gobo

Hi everyone,

I have a python cgi program that uses print statements to write html.
The program has grown, and for reasons I won''t bore you with, I need to
build the page in a string and "print" it at once.

I remember reading somewhere that building the string with successive
"+=" statements is inefficient, but I don''t know any alternatives, and
my search attempts came up empty. Is there a better, more efficient way
to do it than this:

#---------------------------------
htmlPage = "<html><header></header><body>"
htmlPage += "more html"
....
htmlPage += "even more html"
....
htmlPage += "</body></html>"
print htmlPage
#-------------------------------------

Thanks, Gobo

推荐答案

Gobo Borz写道:
Gobo Borz wrote:
我记得在哪里读过建筑的连续的字符串
" + ="声明是低效的,但我不知道任何其他选择,并且我的搜索尝试是空的。是否有更好,更有效的方式来做到这一点:

#----------------------- ----------
htmlPage ="< html>< header>< / header>< body>"
htmlPage + =" more html"
...
htmlPage + ="更多html"
...
htmlPage + ="< / body>< / html>"
print htmlPage
#-------------------------------------
I remember reading somewhere that building the string with successive
"+=" statements is inefficient, but I don''t know any alternatives, and
my search attempts came up empty. Is there a better, more efficient way
to do it than this:

#---------------------------------
htmlPage = "<html><header></header><body>"
htmlPage += "more html"
...
htmlPage += "even more html"
...
htmlPage += "</body></html>"
print htmlPage
#-------------------------------------




最好做点赞/

#--------------------- ------------


htmlbits = []

htmlbits.append("< html>< header> < / header>< body>")

htmlbits.append(" more html")

#...

htmlbits.append(甚至更多html)

#...

htmlbits.append("< / body>< / html>")


#现在为非直观位

打印" .join(htmlbits)


#------------------ ---------------


HTH,


-

alan kennedy

--------------------------------------- --------------

在这里检查http标头: http://xhaus.com/headers

电子邮件alan: http://xhaus.com/mailto/alan


Gobo Borz写道
Gobo Borz wrote


我记得在某个地方用连续的
" + ="来构建字符串。声明是低效的,但我不知道任何其他选择,并且我的搜索尝试是空的。是否有更好,更有效的方式来做到这一点:

#----------------------- ----------
htmlPage ="< html>< header>< / header>< body>"
htmlPage + =" more html"
...
htmlPage + ="更多html"
...
htmlPage + ="< / body>< / html>"
print htmlPage
#-------------------------------------


I remember reading somewhere that building the string with successive
"+=" statements is inefficient, but I don''t know any alternatives, and
my search attempts came up empty. Is there a better, more efficient way
to do it than this:

#---------------------------------
htmlPage = "<html><header></header><body>"
htmlPage += "more html"
...
htmlPage += "even more html"
...
htmlPage += "</body></html>"
print htmlPage
#-------------------------------------




相反,建立一个字符串列表,然后将它们全部加在

结束 - 这样,系统只需要分配

空格一次。更快。


#---------------------------------

htmlPage = []

htmlPage.append("< html>< header>< / header>< body>")

htmlPage.append(" more html")

...

htmlPage.append(甚至更多html)

...

htmlPage.append("< / body>< / html>")

page =''''。join(htmlPage)

打印页面

#------------------------------- ------


Anthony



Instead, build up a list of strings, then join them all at
the end - this way, the system only has to allocate the
space once. Much quicker.

#---------------------------------
htmlPage = []
htmlPage.append("<html><header></header><body>")
htmlPage.append("more html")
...
htmlPage.append("even more html")
...
htmlPage.append("</body></html>")
page = ''''.join(htmlPage)
print page
#-------------------------------------

Anthony


2003年6月25日星期三下午5:20,Gobo Borz写道:
On Wednesday 25 Jun 2003 5:20 pm, Gobo Borz wrote:
大家好,

我有一个python cgi程序,它使用print语句编写html。
程序已经增长,并且由于我赢得了原因没有你,我需要用字符串构建页面并打印。它马上就读了。

我记得在某个地方用连续的
" + ="来构建字符串。声明是低效的,但我不知道任何其他选择,并且我的搜索尝试是空的。是否有更好,更有效的方式来做到这一点:

#----------------------- ----------
htmlPage ="< html>< header>< / header>< body>"
htmlPage + =" more html"
...
htmlPage + ="更多html"
...
htmlPage + ="< / body>< / html>"
print htmlPage
#-------------------------------------
<谢谢,Gobo
Hi everyone,

I have a python cgi program that uses print statements to write html.
The program has grown, and for reasons I won''t bore you with, I need to
build the page in a string and "print" it at once.

I remember reading somewhere that building the string with successive
"+=" statements is inefficient, but I don''t know any alternatives, and
my search attempts came up empty. Is there a better, more efficient way
to do it than this:

#---------------------------------
htmlPage = "<html><header></header><body>"
htmlPage += "more html"
...
htmlPage += "even more html"
...
htmlPage += "</body></html>"
print htmlPage
#-------------------------------------

Thanks, Gobo



一个简单的方法,只需要很少的修改就可以使用

列表并将其连接成一个字符串最后一口气:


----------- 8< --------------

htmlPage = ["< html>< header>< / header>< body> \ n"]

htmlPage.append(" more html \\\
" ;)

....

htmlPage.append(" more html \ n")

....

htmlPage.append("< \ body>< \html> \ n")

h tmlDoc ="" .join(htmlPage)

----------- 8< --------------


这样效率更高,因为字符串连接都是在同一时间完成的,而不是以小块为单位。


希望有所帮助

-andyj


An easy way, which requires little changes tou your code would be to use a
list and join it up into a string in one go at the end:

-----------8<--------------
htmlPage=["<html><header></header><body>\n"]
htmlPage.append("more html\n")
....
htmlPage.append("more html\n")
....
htmlPage.append("<\body><\html>\n")
htmlDoc="".join(htmlPage)
-----------8<--------------

This is more efficient, as the string joining is done all at the same time,
rather than in little chunks.

hope that helps
-andyj


这篇关于更好的方法:myPage + ='more html',...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆