1

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!

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much Jcomeau. ..thats the problem. The software I am using to extract the links(beautifulsoup) is not the same as I am using to click on pages and navigate(htmlunit).
1

Try something simpler, format strings :)

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

4 Comments

Thank you very much for the answer Sarnold. I think I get it now. I didn't know about the \"%s\" I'll play around with it. Thank you very much.
Lostsoul, the \" are there just because you wanted the " in the HTML output. It could also have been: s = "HtmlAnchor[<a href='%s'>]" % addr -- the ' wouldn't require quoting in the double-quoted string.
The % style of format strings is being deprecated slowly, the preferred new method is to use the format function. "HtmlAnchor[<a href=\"{0}\">]".format(addr) . See here for more info
Thanks JHSaunders, I hadn't heard that yet. Pity, I liked the simplicity of %.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.