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

how to get size of unicode string/string in bytes ?

Hello,

how can I get the number of byte of the string in python?
with "len(string)", it doesn't work to get the size of the string in
bytes if I have the unicode string but just the length. (it only works
fine for ascii/latin1) In data structure, I have to store unicode
string for many languages and must know exactly how big of my string
which is stored so I can read back later.

Many thanks for any suggestion.

cheers!
pattreeya.

Aug 1 '06 #1
7 7601
e.g. I use utf8 as encoding/decoding,
s = "ทดสà¸*บ"
u = s.decode("utf-8")
how can I get size of u ?

pa*******@gmail.com schrieb:
Hello,

how can I get the number of byte of the string in python?
with "len(string)", it doesn't work to get the size of the string in
bytes if I have the unicode string but just the length. (it only works
fine for ascii/latin1) In data structure, I have to store unicode
string for many languages and must know exactly how big of my string
which is stored so I can read back later.

Many thanks for any suggestion.

cheers!
pattreeya.
Aug 1 '06 #2
pa*******@gmail.com wrote:
how can I get the number of byte of the string in python?
with "len(string)", it doesn't work to get the size of the string in
bytes if I have the unicode string but just the length. (it only works
fine for ascii/latin1) In data structure, I have to store unicode
string for many languages and must know exactly how big of my string
which is stored so I can read back later.
I do not quite know what you could possibly need that for, but AFAICT Python
only uses two different unicode encodings depending on the platform.

If 'sys.maxunicode' is bigger than 65536, you're on a 32 bit unicode platform
(UCS4), otherwise you're on UCS. For UCS4, you can multiply the length of the
unicode string by 4 to get the length of the internal memory buffer, otherwise
multiply it by 2.

Normally, however, you should not need to deal with this kind of detail. Since
you say "read back later", maybe what you actually want is a serialisation of
the unicode string in, say, UTF-8 or something, that you can actually write to
a file and read back.

Stefan
Aug 1 '06 #3
Stefan Behnel wrote:
pa*******@gmail.com wrote:
> how can I get the number of byte of the string in python?
with "len(string)", it doesn't work to get the size of the string in
bytes if I have the unicode string but just the length. (it only works
fine for ascii/latin1) In data structure, I have to store unicode
string for many languages and must know exactly how big of my string
which is stored so I can read back later.

I do not quite know what you could possibly need that for, but AFAICT
Python only uses two different unicode encodings depending on the
platform.
It is very important for relational databases, as these usually constrain
the amount of bytes per column - so you need the size of bytes, not the
number of unicode characters.

Diez
Aug 1 '06 #4
I got the answer. What I need was so simple but I was blinded at that
moment.
Thanks for any suggestion!
--------

f = open("test.csv", rb)
t1 = f.readline()
>>t2 = t1.decode("iso-8859-9") # test with turkish
t2
u'Dur-kalk trafi\u011fi, t\u0131kan\u0131kl\u0131k tehlikesi\n'
>>print t2
Dur-kalk trafigi, tikaniklik tehlikesi
>>len(t2)
39
>>t2 = t1.decode("iso-8859-9")
t2
u'Dur-kalk trafi\u011fi, t\u0131kan\u0131kl\u0131k tehlikesi\n'
>>print t2
Dur-kalk trafigi, tikaniklik tehlikesi
>>len(t2)
39
>>u1 = t2.encode("utf-8")
u1
'Dur-kalk trafi\xc4\x9fi, t\xc4\xb1kan\xc4\xb1kl\xc4\xb1k tehlikesi\n'
>>print u1
Dur-kalk trafigi, tikaniklik tehlikesi
>>len(u1)
43
>>>


Thnx!

Aug 1 '06 #5
Diez B. Roggisch wrote
Stefan Behnel wrote:
>pa*******@gmail.com wrote:
>> how can I get the number of byte of the string in python?
with "len(string)", it doesn't work to get the size of the string in
bytes if I have the unicode string but just the length. (it only works
fine for ascii/latin1) In data structure, I have to store unicode
string for many languages and must know exactly how big of my string
which is stored so I can read back later.
I do not quite know what you could possibly need that for, but AFAICT
Python only uses two different unicode encodings depending on the
platform.

It is very important for relational databases, as these usually constrain
the amount of bytes per column - so you need the size of bytes, not the
number of unicode characters.
So then the easiest thing to do is: take the maximum length of a unicode
string you could possibly want to store, multiply it by 4 and make that the
length of the DB field.

However, I'm pretty convinced it is a bad idea to store Python unicode strings
directly in a DB, especially as they are not portable. I assume that some DB
connectors honour the local platform encoding already, but I'd still say that
UTF-8 is your best friend here.

Stefan
Aug 1 '06 #6
So then the easiest thing to do is: take the maximum length of a unicode
string you could possibly want to store, multiply it by 4 and make that
the length of the DB field.
However, I'm pretty convinced it is a bad idea to store Python unicode
strings directly in a DB, especially as they are not portable. I assume
that some DB connectors honour the local platform encoding already, but
I'd still say that UTF-8 is your best friend here.
It was your assumption that the OP wanted to store the "real"
unicode-strings. A moot point anyway, at it is afaik not possible to get
their contents in byte form (except from a C-extension).

And assuming 4 bytes per character is a bit dissipative I'd say - especially
when you have some 80% ascii-subset in your text as european and american
languages have.

The solution was given before: chose an encoding (utf-8 is certainly the
most favorable one), and compute the byte-string length.

Diez
Aug 1 '06 #7
Diez B. Roggisch wrote:
>So then the easiest thing to do is: take the maximum length of a unicode
string you could possibly want to store, multiply it by 4 and make that
the length of the DB field.
>However, I'm pretty convinced it is a bad idea to store Python unicode
strings directly in a DB, especially as they are not portable. I assume
that some DB connectors honour the local platform encoding already, but
I'd still say that UTF-8 is your best friend here.

It was your assumption that the OP wanted to store the "real"
unicode-strings. A moot point anyway, at it is afaik not possible to get
their contents in byte form (except from a C-extension).
It is possible:
>>u"a\xff\uffff\U0010ffff".encode("unicode-internal")
'a\x00\xff\x00\xff\xff\xff\xdb\xff\xdf'

This encoding is useless though, as you can't use it for reencoding on
another platform. (And it's probably not what the OP intended.)
And assuming 4 bytes per character is a bit dissipative I'd say - especially
when you have some 80% ascii-subset in your text as european and american
languages have.
That would require UTF-32 as an encoding, which Python currently doesn't
have.
The solution was given before: chose an encoding (utf-8 is certainly the
most favorable one), and compute the byte-string length.
Exactly!

Servus,
Walter
Aug 2 '06 #8

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

Similar topics

8
by: Bill Eldridge | last post by:
I'm trying to grab a document off the Web and toss it into a MySQL database, but I keep running into the various encoding problems with Unicode (that aren't a problem for me with GB2312, BIG 5,...
30
by: aurora | last post by:
I have long find the Python default encoding of strict ASCII frustrating. For one thing I prefer to get garbage character than an exception. But the biggest issue is Unicode exception often pop up...
43
by: Vladimir | last post by:
Method UnicodeEncoding.GetMaxByteCount(charCount) returns charCount * 2. Method UTF8Encoding.GetMaxByteCount(charCount) returns charCount * 4. But why that? Look: /* Each Unicode character...
3
by: hunterb | last post by:
I have a file which has no BOM and contains mostly single byte chars. There are numerous double byte chars (Japanese) which appear throughout. I need to take the resulting Unicode and store it in a...
10
by: Nikolay Petrov | last post by:
How can I convert DOS cyrillic text to Unicode
15
by: John Salerno | last post by:
Forgive my newbieness, but I don't quite understand why Unicode is still something that needs special treatment in Python (and perhaps elsewhere). I'm reading Dive Into Python right now, and it...
40
by: apprentice | last post by:
Hello, I'm writing an class library that I imagine people from different countries might be interested in using, so I'm considering what needs to be provided to support foreign languages,...
2
by: willie | last post by:
Martin v. Löwis: Thanks for the thorough explanation. One last question about terminology then I'll go away :) What is the proper way to describe "ustr" below? <type 'unicode'>
5
by: Holger Joukl | last post by:
Hi there, I consider the behaviour of unicode() inconvenient wrt to conversion of non-string arguments. While you can do: u'17.3' you cannot do:
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.