How convert text file between locale encoding and UTF-8?

2005-12-20 Thread davihigh
Dear Friends:

Wondering that is there neat way to do "subject line" in Python? I am
talking about Python 2.4 with Win32 extension installed. The locale can
be any of ANSI defined, for example, zh_CN (CP936) or Korea (CP949)
.

I am not expert in Python, if you well note I will appreciate a lot.

Rgds, David Xiao

-- 
http://mail.python.org/mailman/listinfo/python-list


print UTF-8 file with BOM

2005-12-22 Thread davihigh
Hi Friends:

fileObj = codecs.open( filename, "r", "utf-8" )
u = fileObj.read() # Returns a Unicode string from the UTF-8 bytes in
the file
print u

It says error:
UnicodeEncodeError: 'gbk' codec can't encode character u'\ufeff' in
position 0:
illegal multibyte sequence

I want to know how read from UTF-8 file, and convert to specified
locale (default is current system locale) and print out string. I hope
put away BOM header automatically.

Rgds, David

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: print UTF-8 file with BOM

2005-12-23 Thread davihigh
FYI. I had just receive something from a friend, he give me following
nice example!

I have one more question on this: How to write if I want to specify
locale other than current locale? For example, program runn on Korea
locale system, and try reading a UTF-8 file that save chinese
characters.

-- The code is here 
import codecs
def read_utf8_txt_file (filename):
fileObj = codecs.open( filename, "r", "utf-8" )
content = fileObj.read()
content = content[1:] #exclude BOM
print content
fileObj.close()

-- 
http://mail.python.org/mailman/listinfo/python-list


MozillaCookieJar doesn't work as expect

2007-02-03 Thread davihigh
OS: WinXP, Python 2.4 latest version

I found it doesn't actually load cookies from the exist cookies.txt.
Instead, it generate a new one when visit that url. Who can help me
check what's problem in following code?

Many thanks in advance!

cj = cookielib.MozillaCookieJar()
cj.load("d:\\temp\\cookies.txt") # I want to use existing cookie
value, not new generated
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)

# then, user page
r = urllib2.urlopen("http://x_page_that_need_loaded_cookie";)
results = r.read()
open('user.html', 'w').write(results)

# save cookies to file
cj.save("./cookies") # with saved cookies, it is NOT the load one

-- 
http://mail.python.org/mailman/listinfo/python-list