Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[html2text] what does it mean sys.stdout in this code snippet 1

Status
Not open for further replies.

medreda

Programmer
Nov 25, 2010
10
FR
Hello again,
the html2text library really helped me and i want to understand every instruction of it, so the problem is
i really does not understand what is the goal of using sys.stdout in this code snippet.
and could i get rid of it


Code:
import sys, urllib
from StringIO import StringIO
import html2text

if __name__ == '__main__':
    url = '[URL unfurl="true"]http://www.fsmb.org'[/URL]
    encoding = 'utf-8'
    f = urllib.urlopen(url)
    try: s = f.read()
    finally: f.close()
    ustr = s.decode(encoding)
    b = StringIO()
    old = sys.stdout
    try:
        sys.stdout = b
        html2text.wrapwrite(html2text.html2text(ustr, url))
    finally: 
            sys.stdout = old
    text = b.getvalue()
    b.close()
    print text

thank you in advance

for more information, we have made a short discussion about that in this topic:
 
medreda said:
i really does not understand what is the goal of using sys.stdout

It's simple about redefining the system stdout - see this example
redef_sys_stdout.py
Code:
[COLOR=#a020f0]import[/color] sys
[COLOR=#a020f0]from[/color] StringIO [COLOR=#a020f0]import[/color] StringIO

[COLOR=#0000ff]# default sys.stdout is screen, [/color]
[COLOR=#0000ff]# so everything you print goes to the screen[/color]
[COLOR=#008080]print[/color] [COLOR=#ff00ff]"This 'Hello' goes to Screen"[/color]

[COLOR=#0000ff]# backup original sys.stdout[/color]
old = sys.stdout
[COLOR=#0000ff]# and redefine sys.sdout[/color]
my_output = StringIO()
sys.stdout = my_output

[COLOR=#0000ff]# now everything you print goes to my_output[/color]
[COLOR=#0000ff]# you don't see it on the screen[/color]
[COLOR=#008080]print[/color] [COLOR=#ff00ff]"This 'Hello' goes to my_output"[/color]
[COLOR=#008080]print[/color] [COLOR=#ff00ff]"... and this too"[/color]

[COLOR=#0000ff]# store the value of my_output into string[/color]
my_str = my_output.getvalue()

[COLOR=#0000ff]# now restore original sys.stdout again,[/color]
[COLOR=#0000ff]# so you can print to the screen[/color]
sys.stdout = old
[COLOR=#0000ff]# and close my_output[/color]
my_output.close()

[COLOR=#0000ff]# process the string[/color]
[COLOR=#008080]print[/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]Now the string contains this:"[/color]
[COLOR=#008080]print[/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff]%s[/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff]"[/color] % my_str
when you run it - you get
Code:
c:\_mikrom\Work>python redef_sys_stdout.py
This 'Hello' goes to Screen

Now the string contains this:
"This 'Hello' goes to my_output
... and this too
"

c:\_mikrom\Work>
 
thanks a lot mikrom
you are really ver yexperienced in python
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top