473,385 Members | 1,769 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,385 software developers and data experts.

Validate string as UTF-8?

I'd like to have a fast way to validate large amounts of string data as
being UTF-8.

I don't see a fast way to do it in Python, though:

unicode(s,'utf-8').encode('utf-8)

seems to notice at least some of the time (the unicode() part works but
the encode() part bombs). I don't consider a RE based solution to be
fast. GLib provides a routine to do this, and I am using GTK so it's
included in there somewhere, but I don't see a way to call GLib
routines. I don't want to write another extension module.

Is there a (fast) Python function to validate UTF-8 data?

Is there some other fast way to validate UTF-8 data?

Is there a general way to call GLib functions?
__________________________________________________ ______________________
TonyN.:' *firstname*nlsnews@georgea*lastname*.com
' <http://www.georgeanelson.com/>
Nov 6 '05 #1
6 6902
On Sun, 06 Nov 2005 18:58:50 +0000, Tony Nelson wrote:

[snip]
Is there a general way to call GLib functions?


ctypes?
http://starship.python.net/crew/theller/ctypes/

Nov 6 '05 #2
Tony Nelson wrote:
I'd like to have a fast way to validate large amounts of string data as
being UTF-8.
define "validate".
I don't see a fast way to do it in Python, though:

unicode(s,'utf-8').encode('utf-8)


if "validate" means "make sure the byte stream doesn't use invalid
sequences", a plain

unicode(s, "utf-8")

should be sufficient.

</F>

Nov 6 '05 #3
Tony Nelson wrote:
I'd like to have a fast way to validate large amounts of string data as
being UTF-8.

I don't see a fast way to do it in Python, though:

unicode(s,'utf-8').encode('utf-8)

seems to notice at least some of the time (the unicode() part works but
the encode() part bombs). I don't consider a RE based solution to be
fast. GLib provides a routine to do this, and I am using GTK so it's
included in there somewhere, but I don't see a way to call GLib
routines. I don't want to write another extension module.


I somehow doubt that the encode bombs. Can you provide some more
details? Maybe of some allegedly not working strings?

Besides that, it's unneccessary - the unicode(s, "utf-8") should be
sufficient. If there are any undecodable byte sequences in there, that
should find them.

Regards,

Diez
Nov 6 '05 #4
In article <pa****************************@gnx.it>,
david mugnai <as*******@gnx.it> wrote:
On Sun, 06 Nov 2005 18:58:50 +0000, Tony Nelson wrote:

[snip]
Is there a general way to call GLib functions?


ctypes?
http://starship.python.net/crew/theller/ctypes/


Umm. Might be easier to write an extension module.
__________________________________________________ ______________________
TonyN.:' *firstname*nlsnews@georgea*lastname*.com
' <http://www.georgeanelson.com/>
Nov 6 '05 #5
I have done this using a sytem call to the program "recode". Recode a
file UTF-8 and do a diff on the original and recoded files. Not an
elegant solution but did seem to function properly.

Take care,

Waitman Gobble

Nov 6 '05 #6
In article <ma**************************************@python.o rg>,
"Fredrik Lundh" <fr*****@pythonware.com> wrote:
Tony Nelson wrote:
I'd like to have a fast way to validate large amounts of string data as
being UTF-8.


define "validate".


All data conforms to the UTF-8 encoding format. I can stand if someone
has made data that impersonates UTF-8 that isn't really Unicode.

I don't see a fast way to do it in Python, though:

unicode(s,'utf-8').encode('utf-8)


if "validate" means "make sure the byte stream doesn't use invalid
sequences", a plain

unicode(s, "utf-8")

should be sufficient.


You are correct. I misunderstood what was happening in my code. I
apologise for wasting bandwidth and your time (and I wasted my own time
as well).

Indeed, unicode(s, 'utf-8') will catch the problem and is fast enough
for my purpose, adding about 25% to the time to load a file.
__________________________________________________ ______________________
TonyN.:' *firstname*nlsnews@georgea*lastname*.com
' <http://www.georgeanelson.com/>
Nov 6 '05 #7

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

Similar topics

0
by: Ray Tayek | last post by:
hi, fooling around with xmlspy (which seems pretty broken when *doing* xslt's). trying to validate in java using code from http://cermics.enpc.fr/doc/java/j2eetutorial-1.4/doc/JAXPSAX13.html...
8
by: johnsocs | last post by:
How do you validate the following XML document, I'm having problems with element 'one' with the attribute xsi:type="xsd:string" <?xml version="1.0" encoding="UTF-8"?> <zero...
1
by: Jonas Bush | last post by:
I'm trying to validate an XmlDocument with an XSD schema, and I'm getting the following error: Could not find schema information for the element 'month'. An error occurred at , (1, 40). Here...
1
by: John Smith | last post by:
Ok, I have 2 xml files that are really similar. They have the exact same structure exept that in one XML some element are required and in the other they are simply absent from the xml. Problem...
1
by: aevans1108 | last post by:
Greetings All If this is the wrong place to post this question, please give me a push in the right direction. Thanks. I know there has to be a simpler way to do this, but this is as simple a...
6
by: Jonny | last post by:
Hi, I'm trying to validate my xml against a xsd but I can't get it to work. Originally, I wanted to validate an xml string but since I didn't get that to work I tried to validate an xml file...
1
by: emma_middlebrook | last post by:
Hi XmlTextReader reader = new XmlTextReader("Accounts.xsd"); XmlSchema schema = XmlSchema.Read(reader, null); schema.Compile(null); XmlDocument d = new XmlDocument();...
3
by: c676228 | last post by:
Hi everyone, I just realized that it's so important to validate each string, I mean 'each' before you insert data from asp page into database. I guess some customers just copy data from some...
1
by: S | last post by:
Hi, I have the following xml file <?xml version="1.0" encoding="UTF-8" ?> <n1:Message xmlns:n1="nz:govt:moh:schemas:diabetes-cardio.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.