Re: create a string of variable lenght
On Mon, Feb 1, 2010 at 12:00 PM, Tracubik wrote: > Il Sun, 31 Jan 2010 19:54:17 -0500, Benjamin Kaplan ha scritto: > >> First of all, if you haven't read this before, please do. It will make >> this much clearer. >> http://www.joelonsoftware.com/articles/Unicode.html > > i'm reading it right now, thanks :-) > > [cut] > >> Solution to your problem: in addition to keeping the #-*- coding ... >> line, go with Günther's advice and use Unicode strings. > > that is: always use the "u" operator (i.e. my_name = u"Nico"), right? > > Ciao, > Nico > Short answer: yes. Slightly longer explanation for future reference: This is true for Python 2 but not Python 3. One of the big changes in Python 3 is that strings are Unicode by default because you're not the only one who runs into this problem. So in Python 3, just writing 'Nico' will make a Unicode string and you have to explicitly declare b'Nico' if you want to look at it as a series of bytes. -- http://mail.python.org/mailman/listinfo/python-list
Re: create a string of variable lenght
Il Sun, 31 Jan 2010 19:54:17 -0500, Benjamin Kaplan ha scritto: > First of all, if you haven't read this before, please do. It will make > this much clearer. > http://www.joelonsoftware.com/articles/Unicode.html i'm reading it right now, thanks :-) [cut] > Solution to your problem: in addition to keeping the #-*- coding ... > line, go with Günther's advice and use Unicode strings. that is: always use the "u" operator (i.e. my_name = u"Nico"), right? Ciao, Nico -- http://mail.python.org/mailman/listinfo/python-list
Re: create a string of variable lenght
On Sun, Jan 31, 2010 at 5:12 PM, Tracubik wrote: > Il Sun, 31 Jan 2010 13:46:16 +0100, Günther Dietrich ha > scritto: > >> Maybe you might solve this if you decode your string to unicode. >> Example: >> >> |>>> euro = "€" >> |>>> len(euro) >> |3 >> |>>> u_euro = euro.decode('utf_8') >> |>>> len(u_euro) >> |1 >> >> Adapt the encoding ('utf_8' in my example) to whatever you use. >> >> Or create the unicode string directly: >> >> |>>> u_euro = u'€' >> |>>> len(u_euro) >> |1 >> >> >> >> Best regards, >> >> Günther > > thank you, your two solution is really interesting. > is there a possible to set unicode encoding by default for my python > scripts? > i've tried inserting > # -*- coding: utf-8 -*- > > at the beginning of my script but doesn't solve the problem First of all, if you haven't read this before, please do. It will make this much clearer. http://www.joelonsoftware.com/articles/Unicode.html To reiterate: UTF-8 IS NOT UNICODE In Python 2, '*' signifies a byte string. It is read as a sequence of bytes and interpreted as a sequence of bytes When Python encounters the sequence 0x27 0xe2 0x82 0xac 0x27 in the code (the UTF-8 bytes for '€') it interprets it as 3 bytes between the two quotes. It doesn't care about characters or anything like that. u'*' signifies a Unicode string. Python will attempt to convert the sequence of bytes into a sequence of characters. It can use any encoding for that: cp1252, utf-8, MacRoman, ISO-8859-15. UTF-8 isn't special, it's just one of the few encodings capable of storing all of the possible Unicode characters. What the line at the top says is that the file should be read using UTF-8. Byte strings are still just sequences of bytes- this doesn't affect them. But any Unicode string will be decoded using UTF-8. IF python looks at the above sequence of bytes as a Unicode string, it views the 3 bytes as a single character. When you ask for it's length, it returns the number of characters. Solution to your problem: in addition to keeping the #-*- coding ... line, go with Günther's advice and use Unicode strings. > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: create a string of variable lenght
Tracubik wrote: Il Sun, 31 Jan 2010 13:46:16 +0100, Günther Dietrich ha scritto: Maybe you might solve this if you decode your string to unicode. Example: |>>> euro = "€" |>>> len(euro) |3 |>>> u_euro = euro.decode('utf_8') |>>> len(u_euro) |1 Adapt the encoding ('utf_8' in my example) to whatever you use. Or create the unicode string directly: |>>> u_euro = u'€' |>>> len(u_euro) |1 Best regards, Günther thank you, your two solution is really interesting. is there a possible to set unicode encoding by default for my python scripts? i've tried inserting # -*- coding: utf-8 -*- at the beginning of my script but doesn't solve the problem That tells Python which encoding the file is using, but you still need to save the file in that encoding. -- http://mail.python.org/mailman/listinfo/python-list
Re: create a string of variable lenght
Il Sun, 31 Jan 2010 13:46:16 +0100, Günther Dietrich ha scritto: > Maybe you might solve this if you decode your string to unicode. > Example: > > |>>> euro = "€" > |>>> len(euro) > |3 > |>>> u_euro = euro.decode('utf_8') > |>>> len(u_euro) > |1 > > Adapt the encoding ('utf_8' in my example) to whatever you use. > > Or create the unicode string directly: > > |>>> u_euro = u'€' > |>>> len(u_euro) > |1 > > > > Best regards, > > Günther thank you, your two solution is really interesting. is there a possible to set unicode encoding by default for my python scripts? i've tried inserting # -*- coding: utf-8 -*- at the beginning of my script but doesn't solve the problem -- http://mail.python.org/mailman/listinfo/python-list
Re: create a string of variable lenght
Tracubik wrote: >i want to print on linux console (terminal) a message like this one: > > >error message of variable lenght > > >to print the asterisks line i do this: > >def StringOfAsterisks(myString): >asterisksString = "*" >for i in range(1,len(myString): > asterisksString += "*" >print asterisksString def StringOfAsterisks(myString): print(''.ljust(len(myString), '*')) You might use string methods .rjust() or .center() instead of method .ljust(). >the code seem to work for me, but it doesn't work properly if in errorMsg >there is some "esotic" char like euro char (€): > euro = "€" len(euro) >3 Maybe you might solve this if you decode your string to unicode. Example: |>>> euro = "€" |>>> len(euro) |3 |>>> u_euro = euro.decode('utf_8') |>>> len(u_euro) |1 Adapt the encoding ('utf_8' in my example) to whatever you use. Or create the unicode string directly: |>>> u_euro = u'€' |>>> len(u_euro) |1 Best regards, Günther -- http://mail.python.org/mailman/listinfo/python-list
Re: create a string of variable lenght
Tracubik wrote: > Hi all, > > i want to print on linux console (terminal) a message like this one: > > > error message of variable lenght > > > to print the asterisks line i do this: > > def StringOfAsterisks(myString): > asterisksString = "*" > for i in range(1,len(myString): >asterisksString += "*" > print asterisksString > > so i create the asterisksString of the lenght i need > > There is a better way of creating asterisksString? > >>> "*" * 10 '**' > the code seem to work for me, but it doesn't work properly if in errorMsg > there is some "esotic" char like euro char (€): > euro = "€" len(euro) > 3 > > how i can solve this? This is less likely to fail when you use unicode strings: >>> def print_message(s): ... print "*" * len(s) ... print s ... print "*" * len(s) ... >>> print_message(u"You are leaving the € zone") ** You are leaving the € zone ** Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: create a string of variable lenght
Tracubik wrote: error message of variable lenght to print the asterisks line i do this: def StringOfAsterisks(myString): asterisksString = "*" for i in range(1,len(myString): asterisksString += "*" print asterisksString so i create the asterisksString of the lenght i need There is a better way of creating asterisksString? well, to make it more pythonic (ignoring camel-case variable-names for now), I'd just use print '*' * len(myString) print myString print '*' * len(myString) possibly stashing the resulting asterisksString once: asterisks_string = '*' * len(my_string) print asterisks_string print my_string print asterisks_string If I used it in multiple places, I might wrap it in a function and/or define a "DIVIDER_CHARACTER" constant, something like DIVIDER_CHARACTER = '*' def surround(s, divider=DIVIDER_CHARACTER): d = divider[0] print d * len(s) print s print d * len(s) surround('hello') surround('world', '-') surround('foo', 'xo') depending on the sort of behavior you want the code seem to work for me, but it doesn't work properly if in errorMsg there is some "esotic" char like euro char (€): euro = "€" len(euro) 3 I suspect you're seeing the byte-representation of your string with a particular-yet-undisclosed encoding. If it was a unicode string (a good idea to specify the encoding at the top of your file), the length should be accurate, so what happens if you >>> euro = u"€" >>> len(euro) ? (I don't have ready access to a terminal where I can enter unicode characters, and you don't display the representation of the string with print repr(euro) so I can steal the byte values to recreate it; but I suspect the result will correctly be 1). -tkc -- http://mail.python.org/mailman/listinfo/python-list
create a string of variable lenght
Hi all, i want to print on linux console (terminal) a message like this one: error message of variable lenght to print the asterisks line i do this: def StringOfAsterisks(myString): asterisksString = "*" for i in range(1,len(myString): asterisksString += "*" print asterisksString so i create the asterisksString of the lenght i need There is a better way of creating asterisksString? the code seem to work for me, but it doesn't work properly if in errorMsg there is some "esotic" char like euro char (€): >>> euro = "€" >>> len(euro) 3 how i can solve this? Thank you all in advance and sorry for my bad english Nico -- http://mail.python.org/mailman/listinfo/python-list