Unable to decode file written by C++ wostringstream

Yan Cheng CHEOK yccheok at yahoo.com
Thu Dec 23 03:33:26 EST 2010


Currently, I have the following text file (https://sites.google.com/site/yanchengcheok/Home/TEST.TXT?attredirects=0&d=1) written by C++ wostringstream.

What I want to do it, I want to write a python script which accept user browser request, and then send over the entire file for user to download. The downloaded file, should be exactly same as the original text file inside server itself.

The code is written as follow :

    import cgi

    print "Content-Type: text/plain"
    print "Content-Disposition: attachment; filename=TEST.txt"
    print

    filename = "C:\\TEST.TXT"
    f = open(filename, 'r')
    for line in f:
        print line

However, when I open up the downloaded file, the file is all having weird characters. I try to use rb flag, it doesn't either.

Is there anything I had missed out? What I wish is, the file (TEST.TXT) downloaded by the client by making query to the above script, will be exactly same as the one in server.

I also try to specific the encoding explicitly.

    import cgi

    print "Content-Type: text/plain; charset=UTF-16"
    print "Content-Disposition: attachment; filename=TEST.txt"
    print

    filename = "C:\\TEST.TXT"
    f = open(filename, 'r')
    for line in f:
        print line.encode('utf-16')

It doesn't work either. Here is the screen shoot for original text file (http://i.imgur.com/S6SjX.png) and file after downloaded from a web browser. (http://i.imgur.com/l39Lc.png)

Is there anything I had missed out?

Thanks and Regards
Yan Cheng CHEOK


      



More information about the Python-list mailing list