encoding latin1 to utf-8

H

Harshad Modi

hello ,
I make one function for encoding latin1 to utf-8. but i think it is
not work proper.
plz guide me.

it is not get proper result . such that i got "Belgi�" using this
method, (Belgium) :

import codecs
import sys
# Encoding / decoding functions
def encode(filename):
file = codecs.open(filename, encoding="latin-1")
data = file.read()
file = codecs.open(filename,"wb", encoding="utf-8")
file.write(data)

file_name=sys.argv[1]
encode(file_name)
 
J

J. Clifford Dyer

Path: news.xs4all.nl!newsspool.news.xs4all.nl!transit.news.xs4all.nl!newsgate.cistron.nl!xs4all!news.glorb.com!postnews.google.com!22g2000hsm.googlegroups.com!not-for-mail

hello ,
I make one function for encoding latin1 to utf-8. but i think it is
not work proper.
plz guide me.

it is not get proper result . such that i got "Belgi???" using this
method, (Belgium) :

import codecs
import sys
# Encoding / decoding functions
def encode(filename):
file = codecs.open(filename, encoding="latin-1")
data = file.read()
file = codecs.open(filename,"wb", encoding="utf-8")
file.write(data)

file_name=sys.argv[1]
encode(file_name)

Some tips to help you out.

1. Close your filehandles when you're done with them.
2. Don't shadow builtin names. Python uses the name file, and binding it to your own function can have ugly side effects that manifest down the road.

So perhaps try the following:

import codecs

def encode(filename):
read_handle = codecs.open(filename, encoding='latin-1')
data = read_handle.read()
read_handle.close()
write_handle = codecs.open(filename, 'wb', encoding='utf-8')
write_handle.write(data)
write_handle.close()

For what it's worth though, I couldn't reproduce your problem with either your code or mine. This is not too surprising as all the ascii characters are encoded identically in utf-8 and latin-1. So your program should output exactly the same file as it reads, if the contents of the file just read "Belgium"

Cheers,
Cliff
 
H

Harshad Modi

thx for Reply ,
but I need some basic knowledge . how to encoding ? which algorithm
use for that . bz my data has some special char , i have not
confidence this function got proper result. i want to make my own
function / script for encoding.
 
C

Carsten Haese

thx for Reply ,
but I need some basic knowledge . how to encoding ? which algorithm
use for that . bz my data has some special char , i have not
confidence this function got proper result. i want to make my own
function / script for encoding.

For basic knowledge about Unicode and character encodings, I highly
recommend amk's excellent Unicode How-To here:
http://www.amk.ca/python/howto/unicode

Once you've read and understood the How-To, I suggest you examine the
following:

1) Are you *sure* that the special characters in the original file are
latin-1 encoded? (If you're not sure, try to look at the file in a HEX
editor to see what character codes it uses for the special characters).
2) Are you sure that what you were using to look at the result file
understands and uses UTF-8 encoding? How are you telling it to use UTF-8
encoding?

Hope this helps,
 
P

Piet van Oostrum

Harshad Modi said:
HM> hello ,
HM> I make one function for encoding latin1 to utf-8. but i think it is
HM> not work proper.
HM> plz guide me.
HM> it is not get proper result . such that i got "Belgi�" using this
HM> method, (Belgium) :
HM> import codecs
HM> import sys
HM> # Encoding / decoding functions
HM> def encode(filename):
HM> file = codecs.open(filename, encoding="latin-1")
HM> data = file.read()
HM> file = codecs.open(filename,"wb", encoding="utf-8")
HM> file.write(data)
HM> file_name=sys.argv[1]
HM> encode(file_name)

I tried this program and for me it works correctly. So you probably used a
wrong input file or you misinterpreted the output. To be sure make hex
dumps of your input/output.
 
H

Harshad Modi

thx for response ,
i think, my file has wrong encoding format.
thanks for guide and advise
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top