简体   繁体   English

将sys.stdout.write输出添加到变量

[英]adding sys.stdout.write output to a variable

Is there a way to add the output of sys.stdout.write to a variable? 有没有办法将sys.stdout.write的输出添加到变量? Or is there a better way to do what I am doing in jython: 或者有更好的方法来做我在jython中做的事情:

I have a web address("www.example.com/whateverpage.html") and I want to create a variable which I get my script to click into so the end result must be: 我有一个网址(“www.example.com/whateverpage.html”),我想创建一个变量,我将我的脚本点击进入,所以最终结果必须是:

HtmlAnchor[<a href="www.example.com/whateverpage.html">]

I tried using pageAnchor = 'HtmlAnchor[<a href="',PageLink,'">]' but it didn't work, because it outputs a space between the href and the PageLink variable. 我尝试使用pageAnchor = 'HtmlAnchor[<a href="',PageLink,'">]'但它不起作用,因为它在href和PageLink变量之间输出一个空格。 So I figured I was smart(which apparently I am far from :-) and used stdout.write to print with the spaces but when I output it in the script it looks perfect but when I try to save it to a variable and then print that variable I get (None, None, None). 所以我认为我很聪明(显然我远离:-)并使用stdout.write打印空格但是当我在脚本中输出它看起来很完美但是当我尝试将其保存到变量然后打印我得到的变量(无,无,无)。

The other way I thought of doing this is to use regexpressions to get rid of spaces but I need the space between 'a' and 'href' 我想这样做的另一种方法是使用regexpressions来摆脱空间,但我需要'a'和'href'之间的空间

I'm sure there is a simple way I'm just not seeing, can anyone give me any pointers.. Thanks! 我确信有一种简单的方法我只是没有看到,有人能给我任何指示......谢谢!

Maybe you want pageAnchor = ''.join(['HtmlAnchor[<a href="',PageLink,'">]']) 也许你想要pageAnchor = ''.join(['HtmlAnchor[<a href="',PageLink,'">]'])

Or simply 'HtmlAnchor[<a href="' + PageLink + '">]' 或者只是'HtmlAnchor[<a href="' + PageLink + '">]'

But it's far from clear, to me, what you're trying to accomplish. 但是,对我来说,你想要实现的目标还远未明朗。

Try something simpler, format strings :) 尝试更简单的东西, 格式化字符串 :)

>>> addr="www.example.com"
>>> s = "HtmlAnchor[<a href=\"%s\">]" % addr
>>> s
'HtmlAnchor[<a href="www.example.com">]'
>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM