×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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 = 'http://www.fsmb.org'
    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:
http://www.tek-tips.com/viewthread.cfm?qid=1604666&page=1
 

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

Quote (medreda):


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

import sys
from StringIO import StringIO

# default sys.stdout is screen,
# so everything you print goes to the screen
print "This 'Hello' goes to Screen"

# backup original sys.stdout
old = sys.stdout
# and redefine sys.sdout
my_output = StringIO()
sys.stdout = my_output

# now everything you print goes to my_output
# you don't see it on the screen
print "This 'Hello' goes to my_output"
print "... and this too"

# store the value of my_output into string
my_str = my_output.getvalue()

# now restore original sys.stdout again,
# so you can print to the screen
sys.stdout = old
# and close my_output
my_output.close()

# process the string
print "\nNow the string contains this:"
print "\"%s\"" % 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>

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

(OP)
thanks a lot mikrom
you are really ver yexperienced in python

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close