473,372 Members | 1,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

encoding latin1 to utf-8

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)

Sep 10 '07 #1
6 9563
On Mon, Sep 10, 2007 at 12:25:46PM -0000, Harshad Modi wrote regarding encoding latin1 to utf-8:
Path: news.xs4all.nl!newsspool.news.xs4all.nl!transit.ne ws.xs4all.nl!newsgate.cistron.nl!xs4all!news.glorb .com!postnews.google.com!22g2000hsm.googlegroups.c om!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
Sep 10 '07 #2
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.

Sep 10 '07 #3
On Mon, 2007-09-10 at 13:11 +0000, Harshad Modi wrote:
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,

--
Carsten Haese
http://informixdb.sourceforge.net
Sep 10 '07 #4
>>>>Harshad Modi <mo******@gmail.com(HM) wrote:
>HMhello ,
HM I make one function for encoding latin1 to utf-8. but i think it is
HMnot work proper.
HMplz guide me.
>HMit is not get proper result . such that i got "Belgi�" using this
HMmethod, (Belgium) :
>HMimport codecs
HMimport sys
HM# Encoding / decoding functions
HMdef 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)
>HMfile_name=sys.argv[1]
HMencode(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.
--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
Sep 10 '07 #5
On Sep 10, 5:25 am, Harshad Modi <modii...@gmail.comwrote:
hello ,
I make one function for encoding latin1 to utf-8. but i think it is
not work proper.
plz guide me.
Hi, what you want is here, including complete code:

Converting a File's "Character Set" / Encoding
http://xahlee.org/perl-python/charset_encoding.html

Xah
xa*@xahlee.org
http://xahlee.org/

Sep 10 '07 #6
thx for response ,
i think, my file has wrong encoding format.
thanks for guide and advise

Sep 12 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Kennedy | last post by:
I have an XML Snippet <?xml version="1.0" encoding="UTF-8"?> and when I convert the entire xml file to a DOM and then generate a new file from the DOM, results in <?xml version="1.0"?>. Any...
9
by: Joe Blow | last post by:
Strange problem, Web pages encoded in utf-8 are appearing on customers' browsers as iso-western european. This means that characters like the British symbol get messed up. No amount of...
0
by: JuanDG | last post by:
Hi, I have a .Net Web Service and the encoding of the SOAP Messages is always UTF-8, and I need to change the web service so that it encodes with ISO-8859-1 because it’s the only encoding my...
2
by: Guillermo Rosich Capablanca | last post by:
I have a problem with utf-8 enconding and I don't know what to do in order to make it work. I want to open a new window with excel data so the user can choose to save it local. Everything...
3
by: JuanDG | last post by:
Hi, I have a .Net Web Service and the encoding of the SOAP Messages is always UTF-8, and I need to change the web service so that it encodes with ISO-8859-1 because it’s the only encoding my...
1
by: Sin Jeong-hun | last post by:
Even though I don't use Korean in my codes and comments, I often need to insert Korean literals, because the target users are Koreans. Because I have no plan to globalize those programs, I use...
3
by: jariwaladivyesh | last post by:
Hi frnds, i have simple XML doc <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="test.xsl"?> <data> <name> Divyesh Jariala</name> </data>
2
by: Jeffrey Walton | last post by:
Hi All, BMP Strings are a subset of Universal Strings.The BMP string uses approximately 65,000 code points from Universal String encoding. BMP Strings: ISO/IEC 10646, 2-octet canonical form,...
8
by: autumnrrr | last post by:
This is coming up Invalid PI name <?XML version="1.0" encoding="UTF-8"?> XML declaration allowed only at the start of the document <?xml version="1.0" encoding="UTF-8"?>
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.