adding sys.stdout.write output to a variable
Is there a way to add the output of sys.stdout.write to a variable? Or is there a better way to do what I am doing in 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:
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. 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).
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'
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,'">]'])
Or simply '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">]'
>>>
精彩评论