Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Unable to decode file written by C++ wostringstream

Reply
Thread Tools

Unable to decode file written by C++ wostringstream

 
 
Yan Cheng CHEOK
Guest
Posts: n/a
 
      12-23-2010
Currently, I have the following text file (https://sites.google.com/site/yanche...edirects=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



 
Reply With Quote
 
 
 
 
jmfauth
Guest
Posts: n/a
 
      12-23-2010
On 23 Dez., 09:33, Yan Cheng CHEOK <(E-Mail Removed)> wrote:
> Currently, I have the following text file (https://sites.google.com/site/yanche...XT?attredirect...) written by C++ wostringstream.
>


The coding of the file is utf-16le. You should take care
of this coding when you *read* the file, and not when
you display its content.

>>> import io
>>> with io.open('test.txt', 'r', encoding='utf-16le') as f:

.... r = f.readlines()
....
>>> len(r)

94
>>> r[:5]

[u'\n', u' 0.000 1.500 3.000 0.526
0.527 0.527 0.00036 0.00109 1381.88
485.07\n', u' 0.000 1.500 3.000 1.084
1.085 1.086 0.00037 0.00111 1351.86
978.02\n', u' 0.000 1.500 3.000
1.166 1.167 1.168 0.00043 0.00130
1152.71 897.16\n', u' -3.000 0.000 3.000
-0.031 -0.029 -0.025 0.00158 0.00475
632.17 626.13\n']
>>> # from there, do what you wish with this list...
>>>


jmf
 
Reply With Quote
 
 
 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      12-23-2010
Yan Cheng CHEOK wrote:
> Currently, I have the following text file
>

(https://sites.google.com/site/yanche...edirects=0&d=1)
> written by C++ wostringstream.


Stringstream? I guess you meant wofstream, or? Anyway, the output encoding
of C++ iostreams is implementation-defined, so you can't assume that such
code is generally portable. If you want a certain encoding, you need to
tell the ofstream using the codecvt facet of the locale, a websearch should
turn up more info on that.

If you have the data in memory and it is encoded as UTF-16 there (which is
what MS Windows uses for its wchar_t) then you could also use a plain
ofstream, open it with the binary flag and then simply write the memory to
a file.

In any case, you need to know the encoding in order to get the content into
a Python string or unicode object, otherwise you will only get garbage.

Good luck!

Uli

--
Domino Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generally, are the programs written by C++ slower than written by C10% ? KaiWen C++ 102 09-14-2011 11:12 PM
fstream data from file, with file periodically written with new data roughtrader C++ 3 02-17-2009 12:42 PM
Combine and Decode yEnc file problem Steve Computer Support 3 04-30-2005 02:59 PM
How do i decode an ISO-8859-1 encoded text file? SHIRE Java 4 02-04-2004 04:03 PM
Re: Can a usercontrol written in C# be used in Web Forms that is written in VB.Net? Steve C. Orr, MCSD ASP .Net 1 08-24-2003 12:06 AM



Advertisments