Re: [Tutor] unicode help

2012-11-14 Thread Sander Sweers
Marilyn Davis schreef op wo 14-11-2012 om 13:23 [-0800]:
> I found this site:
> http://hints.macworld.com/article.php?story=20100713130450549
> 
> and that fixes it.

Short answer: It is not a fix but a workaround. Try:
print symbol.encode('utf-8')

Longer answer: It is not really a fix, it is a workaround for the
implicit encode/decode python does. Setting this environment variable
will make the errors go away for *you* but what about the other people
running the code. What you are seeing is the *implicit* encode from a
unicode string to a byte string using sys.getdefaultencoding. What you
need to do is encode your *unicode* string to a *byte* string by using
encode() method. You should watch/read
http://nedbatchelder.com/text/unipain.html and see how to fix this
properly.

This quick interactive session shows how this error is properly solved.

$ python
Python 2.7.3 (default, Sep 22 2012, 18:13:33) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'ascii'
>>> unicode_symbol = unichr(165)
>>> print unicode_symbol
Traceback (most recent call last):
  File "", line 1, in 
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa5' in
position 0: ordinal not in range(128)
>>> byte_symbol = unicode_symbol.encode('utf-8')
>>> print byte_symbol
¥

I do not use macosx but I suspect it will support UTF-8, however if not
you need to find an encoding that is supported and has your character.

This can be quite confusing so I really suggest strongly to watch Ned's
talk ;-).

Greets
Sander

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode help

2012-11-14 Thread Dave Angel
On 11/14/2012 04:07 PM, Marilyn Davis wrote:
> 
>
> Goodness!  I didn't expect it to be a Mac thing.
>
> So, on a Windows machine, running Python 2.6.6, sys.stdout.encoding is
> 'cp1252', yet the code runs fine.
>
> On Ubuntu with 2.7, it's 'UTF-8' and  it runs just fine.
>
> I find this most mysterious.
>
> Thank you for any help to get it running on my Mac.
>
>

To resolve something like this, I'd start searching the internet (and
especially the python.org site) for a string like:
 python  sys.stdout.encoding

https://drj11.wordpress.com/2007/05/14/python-how-is-sysstdoutencoding-chosen/

According to this page, the encoding is chosen by the environment variable:
 LC_CTYPE

set LC_CTYPE=en_GB.utf-8


I have no idea if that's correct, or always correct, but it could be
worth experimenting.  (Later note:  I think this affects more than just
the terminal, so I'd skip it)

In the meantime, you  posted a link to

http://hints.macworld.com/article.php?story=20100713130450549

which suggests two different environment variables:  PYTHONIOENCODING and 
LC-CTYPE


Looking at the python.org site is presumably more authoritative.  See

http://docs.python.org/2/whatsnew/2.6.html?highlight=pythonioencoding

which says """
The encoding used for standard input, output, and standard error can be 
specified by setting the PYTHONIOENCODING 
 
environment variable before running the interpreter. The value should be a 
string in the form  or :. The /encoding/ part 
specifies the encoding’s name, e.g. utf-8 or latin-1; the optional 
/errorhandler/ part specifies what to do with characters that can’t be handled 
by the encoding, and should be one of “error”, “ignore”, or “replace”. 
(Contributed by Martin von Loewis.)"""


Also:  
http://docs.python.org/2/using/cmdline.html?highlight=pythonioencoding#PYTHONIOENCODING
 


Anyway, just realize that this does NOT change the terminal.  If it's not 
really utf-8, you'll sometimes get garbage characters. But at least you 
shouldn't get the encoding errors.

The *right* answer I figure would be to experiment to find out what the 
terminal on your Mac actually uses, and use that in your environment variable.


-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode help

2012-11-14 Thread Marilyn Davis
On Wed, November 14, 2012 1:07 pm, Marilyn Davis wrote:

> Thank you, Dave, for looking at my problem, and for correcting me on my
> top posting.
>
> See below:
>
>
> On Wed, November 14, 2012 12:34 pm, Dave Angel wrote:
>
>
>> On 11/14/2012 03:10 PM, Marilyn Davis wrote:
>>
>>
>>> Hi,
>>>
>>>
>>>
>>> Last year, I was helped so that this ran nicely on my 2.6:
>>>
>>>
>>>
>>> #! /usr/bin/env python
>>> # -*- coding: utf-8 -*-
>>> # necessary for python not to complain about "¥"
>>>
>>>
>>>
>>> symbol = unichr(165) print unicode(symbol)
>>>
>>> --- end of code ---
>>>
>>>
>>>
>>> But, now on my 2.7, and on 2.6 when I tried reinstalling it, I get:
>>>
>>>
>>>
>>> bash-3.2$ ./uni_test.py ./uni_test.py Traceback (most recent call
>>> last):
>>> File "./uni_test.py", line 6, in 
>>> print unicode(symbol) UnicodeEncodeError: 'ascii' codec can't encode
>>> character u'\xa5' in position 0: ordinal not in range(128) bash-3.2$
>>>
>>> Can anyone please help?  It says 'ascii' codec?  Shouldn't it be
>>> seeing 'utf-8'?
>>>
>>>
>>>
>>> I can't imagine it matters but I'm on an imac now and before I was on
>>>  Ubuntu.
>>>
>>>
>>>
>>> Thank you,
>>>
>>>
>>>
>>> Marilyn Davis
>>>
>>>
>>>
>>>
>>>
>>>
>>
>> You top-posted your message.  If you need to quote something, please
>> put your new text after what you're quoting.
>>
>> Try the following in your 2.7 interpreter:
>>
>>
>>
> import sys print sys.stdout.encoding
>> UTF-8
>>
>>
>>
>> If you don't see UTF-8, then there's a discrepancy between what you
>> think the terminal is doing, and what Python thinks.
>
> You're right, I get this:
>
>
> bash-3.2$ python Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43)
>  [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
>
 import sys
> import sys
 print sys.stdout.encoding
> print sys.stdout.encoding US-ASCII
>

>
>
>>
>> Somebody familiar with the Mac might be able to tell you how to fix it
>> right, but you could try sys.stdout.encoding = "UTF-8"
>>
>> and see if it changes your symptoms.
>
> and, your good idea didn't work:
>
 sys.stdout.encoding="UTF-8"
> sys.stdout.encoding="UTF-8" Traceback (most recent call last):
> File "", line 1, in 
> TypeError: readonly attribute
>

>
> Goodness!  I didn't expect it to be a Mac thing.
>
>
> So, on a Windows machine, running Python 2.6.6, sys.stdout.encoding is
> 'cp1252', yet the code runs fine.
>
>
> On Ubuntu with 2.7, it's 'UTF-8' and  it runs just fine.
>
>
> I find this most mysterious.
>
>
> Thank you for any help to get it running on my Mac.
>
>
> Marilyn
>

I found this site:
http://hints.macworld.com/article.php?story=20100713130450549

and that fixes it.

I would never guessed that!

Marilyn

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode help

2012-11-14 Thread Marilyn Davis
Thank you, Dave, for looking at my problem, and for correcting me on my
top posting.

See below:

On Wed, November 14, 2012 12:34 pm, Dave Angel wrote:

> On 11/14/2012 03:10 PM, Marilyn Davis wrote:
>
>> Hi,
>>
>>
>> Last year, I was helped so that this ran nicely on my 2.6:
>>
>>
>> #! /usr/bin/env python
>> # -*- coding: utf-8 -*-
>> # necessary for python not to complain about "¥"
>>
>>
>> symbol = unichr(165) print unicode(symbol)
>>
>> --- end of code ---
>>
>>
>> But, now on my 2.7, and on 2.6 when I tried reinstalling it, I get:
>>
>>
>> bash-3.2$ ./uni_test.py ./uni_test.py
>> Traceback (most recent call last):
>> File "./uni_test.py", line 6, in 
>> print unicode(symbol) UnicodeEncodeError: 'ascii' codec can't encode
>> character u'\xa5' in position 0: ordinal not in range(128) bash-3.2$
>>
>> Can anyone please help?  It says 'ascii' codec?  Shouldn't it be seeing
>>  'utf-8'?
>>
>>
>> I can't imagine it matters but I'm on an imac now and before I was on
>> Ubuntu.
>>
>>
>> Thank you,
>>
>>
>> Marilyn Davis
>>
>>
>>
>>
>>
>
> You top-posted your message.  If you need to quote something, please put
> your new text after what you're quoting.
>
> Try the following in your 2.7 interpreter:
>
>
 import sys print sys.stdout.encoding
> UTF-8
>
>
> If you don't see UTF-8, then there's a discrepancy between what you
> think the terminal is doing, and what Python thinks.

You're right, I get this:

bash-3.2$ python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
import sys
>>> print sys.stdout.encoding
print sys.stdout.encoding
US-ASCII
>>>


>
> Somebody familiar with the Mac might be able to tell you how to fix it
> right, but you could try sys.stdout.encoding = "UTF-8"
>
> and see if it changes your symptoms.

and, your good idea didn't work:

>>> sys.stdout.encoding="UTF-8"
sys.stdout.encoding="UTF-8"
Traceback (most recent call last):
  File "", line 1, in 
TypeError: readonly attribute
>>>

Goodness!  I didn't expect it to be a Mac thing.

So, on a Windows machine, running Python 2.6.6, sys.stdout.encoding is
'cp1252', yet the code runs fine.

On Ubuntu with 2.7, it's 'UTF-8' and  it runs just fine.

I find this most mysterious.

Thank you for any help to get it running on my Mac.

Marilyn





>
> --
>
>
> DaveA


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode help

2012-11-14 Thread Dave Angel
On 11/14/2012 03:10 PM, Marilyn Davis wrote:
> Hi,
>
> Last year, I was helped so that this ran nicely on my 2.6:
>
> #! /usr/bin/env python
> # -*- coding: utf-8 -*-
> # necessary for python not to complain about "¥"
>
> symbol = unichr(165)
> print unicode(symbol)
>
> --- end of code ---
>
> But, now on my 2.7, and on 2.6 when I tried reinstalling it, I get:
>
> bash-3.2$ ./uni_test.py
> ./uni_test.py
> Traceback (most recent call last):
>   File "./uni_test.py", line 6, in 
> print unicode(symbol)
> UnicodeEncodeError: 'ascii' codec can't encode character u'\xa5' in
> position 0: ordinal not in range(128)
> bash-3.2$
>
> Can anyone please help?  It says 'ascii' codec?  Shouldn't it be seeing
> 'utf-8'?
>
> I can't imagine it matters but I'm on an imac now and before I was on Ubuntu.
>
> Thank you,
>
> Marilyn Davis
>
>
>
>

You top-posted your message.  If you need to quote something, please put
your new text after what you're quoting.

Try the following in your 2.7 interpreter:

>>> import sys
>>> print sys.stdout.encoding
UTF-8

If you don't see UTF-8, then there's a discrepancy between what you
think the terminal is doing, and what Python thinks.

Somebody familiar with the Mac might be able to tell you how to fix it
right, but you could try
sys.stdout.encoding = "UTF-8"

and see if it changes your symptoms.


-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode help

2012-11-14 Thread Marilyn Davis
Hi,

Last year, I was helped so that this ran nicely on my 2.6:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# necessary for python not to complain about "¥"

symbol = unichr(165)
print unicode(symbol)

--- end of code ---

But, now on my 2.7, and on 2.6 when I tried reinstalling it, I get:

bash-3.2$ ./uni_test.py
./uni_test.py
Traceback (most recent call last):
  File "./uni_test.py", line 6, in 
print unicode(symbol)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa5' in
position 0: ordinal not in range(128)
bash-3.2$

Can anyone please help?  It says 'ascii' codec?  Shouldn't it be seeing
'utf-8'?

I can't imagine it matters but I'm on an imac now and before I was on Ubuntu.

Thank you,

Marilyn Davis








On Sat, May 28, 2011 2:17 pm, Marilyn Davis wrote:

> Thank you Martin,
>
>
> This:
>
>
> #!/usr/bin/env python
> # -*- coding: utf8 -*-
> '''Unicode handling for 2.6.
> '''
> [rest of module deleted]
>
>
> produces an emacs warning:
>
> Warning (mule): Invalid coding system `utf8' is specified
> for the current buffer/file by the :coding tag. It is highly recommended to
> fix it before writing to a file.
>
> But, if I save anyway, and run, I get this:
>
>
> ./uni.py
> File "./uni.py", line 13
> SyntaxError: 'utf8' codec can't decode byte 0xa5 in position 0: unexpected
>  code byte
>
> but, on a hunch, I tried
>
> # -*- coding: utf-8 -*-
>
>
> and emacs and python were very happy.  Thank you thank you thank you.
>
> Now I can enjoy my Saturday.
>
>
> Marilyn
>
>
>
>
>
> On Sat, May 28, 2011 3:00 pm, Martin A. Brown wrote:
>
>
>> Hello there,
>>
>>
>>
>> : I'm still on Python 2.6 and I'm trying to work some unicode
>> : handling.
>> :
>> : I've spent some hours on this snippet of code, trying to follow
>> : PEP 0263, since the error tells me to see it.  I've tried other
>> : docs too and I am still clueless.
>>
>>
>>
>> OK, so this is PEP 0263.  http://www.python.org/dev/peps/pep-0263/
>>
>>
>>
>> Did you miss these lines?
>>
>>
>>
>> To define a source code encoding, a magic comment must
>> be placed into the source files either as first or second line in the
>> file, such as:
>>
>> Or was it the lack of an explicit example for UTF-8 in the PEP?
>>
>>
>>
>> Try adding a single line to your script, as the second line.  That
>> should make your script look like:
>>
>> #! /usr/bin/env python
>> # -*- coding: utf8 -*-
>>
>>
>>
>> You might wonder why on earth you have to do this.  The interpreter
>> cannot safely assume that your editor (any arbitrary text editor) knows
>> how to create/save anything other than ASCII without this (slightly
>> hackish) hint.
>>
>> Good luck,
>>
>>
>>
>> -Martin
>>
>>
>>
>> --
>> Martin A. Brown
>> http://linux-ip.net/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode help

2011-05-28 Thread Marilyn Davis
Aye, thank you.  I do like that syntax better.

Sometimes it's time to just quit and try again later when I'm not so
frustrated.  That's when I make silly bugs.

But, we got it!

Thank you again.  I think it's a nifty hack.

M


On Sat, May 28, 2011 3:53 pm, Alexandre Conrad wrote:

> Marilyn,
>
>
> You miss-typed the line, it should have a semicolon right after the
> word "coding", such as:
>
> # coding: utf-8
>
>
> not
>
> # coding utf-8
>
>
> as you showed from your file.
>
> The syntax suggested syntax # -*- coding: utf8 -*- by Martin is
> equivalent, but I always have a hard time remembering it from the top of my
> head when I create a new Python file. So I just use: # coding: utf-8
>
>
>
> 2011/5/28 Marilyn Davis :
>
>> Thank you Alexandre for your quick reply.
>>
>>
>> I tried your suggestion (again) and I still get:
>>
>>
>> ./uni.py
>>  File "./uni.py", line 20
>> SyntaxError: Non-ASCII character '\xa5' in file ./uni.py on line 21, but
>>  no encoding declared; see http://www.python.org/peps/pep-0263.html for
>>  details
>>
>> Can you suggest a different encoding?  Or a way to figure out what it
>> should be?
>>
>> Or am I making and re-making some stupid mistake?
>>
>>
>> I run on emacs under SUSE.
>>
>>
>> Marilyn
>>
>>
>> p.s.  The code is now:
>>
>> #!/usr/bin/env python
>> # coding utf-8
>> '''Unicode handling for 2.6.
>> '''
>> class Currency(float):    def __str__(self):
>>        value = self.__class__.symbol +  float.__str__(self)
>>        return value
>>
>>
>> class Yen(Currency):    symbol = unichr(165)
>>
>>
>> def main():    y = Yen(100)
>>    print unicode(y)
>>
>>
>> main()
>>
>> """
>> ¥100.0
>> """
>>
>>
>>
>>
>>
>>
>>
>> On Sat, May 28, 2011 2:21 pm, Alexandre Conrad wrote:
>>
>>
>>> When Python loads your file from your file system, it assumes all
>>> characters in the file are ASCII. But when it hits non-ASCII
>>> characters (currency symbols), Python doesn't know how to interpret
>>> it. So you can give Python a hint by putting at the top of your file
>>> the encoding of your file:
>>>
>>> After the shebang (1st line), add the following comment:
>>> # coding: utf-8
>>>
>>>
>>>
>>> (or whatever encoding your file is saved to, I think it depends on
>>> your file system, usually utf-8 by default on Linux)
>>>
>>> HTH,
>>>
>>>
>>>
>>>
>>> 2011/5/28 Marilyn Davis :
>>>
>>>
 Hi,



 I'm still on Python 2.6 and I'm trying to work some unicode
 handling.


 I've spent some hours on this snippet of code, trying to follow PEP
  0263,
 since the error tells me to see it.  I've tried other docs too and I
 am still clueless.

 The code works, except for the comment at the end.



 I would be very grateful for some help.




 #!/usr/bin/env python
 '''Unicode handling for 2.6.
 '''
 class Currency(float):    def __str__(self):        value =
 self.__class__.symbol +  float.__str__(self)        return value



 class Yen(Currency):    symbol = unichr(165)


 class Pound(Currency):    symbol = unichr(163)


 def main():    y = Yen(100)    print unicode(y)
    p = Pound(100)
    print unicode(p)



 main()

 """
 ¥100.0
 £100.0
 """





 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor



>>>
>>>
>>>
>>> --
>>> Alex | twitter.com/alexconrad
>>>
>>>
>>
>>
>>
>
>
>
> --
> Alex | twitter.com/alexconrad
>



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode help

2011-05-28 Thread Alexandre Conrad
Marilyn,

You miss-typed the line, it should have a semicolon right after the
word "coding", such as:

# coding: utf-8

not

# coding utf-8

as you showed from your file.

The syntax suggested syntax # -*- coding: utf8 -*- by Martin is
equivalent, but I always have a hard time remembering it from the top
of my head when I create a new Python file. So I just use: # coding:
utf-8



2011/5/28 Marilyn Davis :
> Thank you Alexandre for your quick reply.
>
> I tried your suggestion (again) and I still get:
>
> ./uni.py
>  File "./uni.py", line 20
> SyntaxError: Non-ASCII character '\xa5' in file ./uni.py on line 21, but
> no encoding declared; see http://www.python.org/peps/pep-0263.html for
> details
>
> Can you suggest a different encoding?  Or a way to figure out what it
> should be?
>
> Or am I making and re-making some stupid mistake?
>
> I run on emacs under SUSE.
>
> Marilyn
>
> p.s.  The code is now:
>
> #!/usr/bin/env python
> # coding utf-8
> '''Unicode handling for 2.6.
> '''
> class Currency(float):
>    def __str__(self):
>        value = self.__class__.symbol +  float.__str__(self)
>        return value
>
> class Yen(Currency):
>    symbol = unichr(165)
>
> def main():
>    y = Yen(100)
>    print unicode(y)
>
> main()
>
> """
> ¥100.0
> """
>
>
>
>
>
>
> On Sat, May 28, 2011 2:21 pm, Alexandre Conrad wrote:
>
>> When Python loads your file from your file system, it assumes all
>> characters in the file are ASCII. But when it hits non-ASCII characters
>> (currency symbols), Python doesn't know how to interpret
>> it. So you can give Python a hint by putting at the top of your file the
>> encoding of your file:
>>
>> After the shebang (1st line), add the following comment:
>> # coding: utf-8
>>
>>
>> (or whatever encoding your file is saved to, I think it depends on
>> your file system, usually utf-8 by default on Linux)
>>
>> HTH,
>>
>>
>>
>> 2011/5/28 Marilyn Davis :
>>
>>> Hi,
>>>
>>>
>>> I'm still on Python 2.6 and I'm trying to work some unicode handling.
>>>
>>>
>>> I've spent some hours on this snippet of code, trying to follow PEP
>>> 0263,
>>> since the error tells me to see it.  I've tried other docs too and I am
>>> still clueless.
>>>
>>> The code works, except for the comment at the end.
>>>
>>>
>>> I would be very grateful for some help.
>>>
>>>
>>>
>>> #!/usr/bin/env python
>>> '''Unicode handling for 2.6.
>>> '''
>>> class Currency(float):    def __str__(self):
>>>        value = self.__class__.symbol +  float.__str__(self)
>>>        return value
>>>
>>>
>>> class Yen(Currency):    symbol = unichr(165)
>>>
>>>
>>> class Pound(Currency):    symbol = unichr(163)
>>>
>>>
>>> def main():    y = Yen(100)
>>>    print unicode(y)
>>>    p = Pound(100)
>>>    print unicode(p)
>>>
>>>
>>> main()
>>>
>>> """
>>> ¥100.0
>>> £100.0
>>> """
>>>
>>>
>>>
>>>
>>> ___
>>> Tutor maillist  -  Tutor@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>>
>>
>>
>>
>> --
>> Alex | twitter.com/alexconrad
>>
>
>
>



-- 
Alex | twitter.com/alexconrad
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode help

2011-05-28 Thread Marilyn Davis
Thank you Martin,

This:

#!/usr/bin/env python
# -*- coding: utf8 -*-
'''Unicode handling for 2.6.
'''
[rest of module deleted]

produces an emacs warning:

Warning (mule): Invalid coding system `utf8' is specified
for the current buffer/file by the :coding tag.
It is highly recommended to fix it before writing to a file.

But, if I save anyway, and run, I get this:

./uni.py
  File "./uni.py", line 13
SyntaxError: 'utf8' codec can't decode byte 0xa5 in position 0: unexpected
code byte

but, on a hunch, I tried

# -*- coding: utf-8 -*-

and emacs and python were very happy.  Thank you thank you thank you.

Now I can enjoy my Saturday.

Marilyn




On Sat, May 28, 2011 3:00 pm, Martin A. Brown wrote:

> Hello there,
>
>
> : I'm still on Python 2.6 and I'm trying to work some unicode
> : handling.
> :
> : I've spent some hours on this snippet of code, trying to follow
> : PEP 0263, since the error tells me to see it.  I've tried other
> : docs too and I am still clueless.
>
>
> OK, so this is PEP 0263.  http://www.python.org/dev/peps/pep-0263/
>
>
> Did you miss these lines?
>
>
> To define a source code encoding, a magic comment must
> be placed into the source files either as first or second line in the file,
> such as:
>
> Or was it the lack of an explicit example for UTF-8 in the PEP?
>
>
> Try adding a single line to your script, as the second line.  That
> should make your script look like:
>
> #! /usr/bin/env python
> # -*- coding: utf8 -*-
>
>
> You might wonder why on earth you have to do this.  The interpreter
> cannot safely assume that your editor (any arbitrary text editor) knows how
> to create/save anything other than ASCII without this (slightly hackish)
> hint.
>
> Good luck,
>
>
> -Martin
>
>
> --
> Martin A. Brown
> http://linux-ip.net/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode help

2011-05-28 Thread Marilyn Davis
Thank you Alexandre for your quick reply.

I tried your suggestion (again) and I still get:

./uni.py
  File "./uni.py", line 20
SyntaxError: Non-ASCII character '\xa5' in file ./uni.py on line 21, but
no encoding declared; see http://www.python.org/peps/pep-0263.html for
details

Can you suggest a different encoding?  Or a way to figure out what it
should be?

Or am I making and re-making some stupid mistake?

I run on emacs under SUSE.

Marilyn

p.s.  The code is now:

#!/usr/bin/env python
# coding utf-8
'''Unicode handling for 2.6.
'''
class Currency(float):
def __str__(self):
value = self.__class__.symbol +  float.__str__(self)
return value

class Yen(Currency):
symbol = unichr(165)

def main():
y = Yen(100)
print unicode(y)

main()

"""
¥100.0
"""






On Sat, May 28, 2011 2:21 pm, Alexandre Conrad wrote:

> When Python loads your file from your file system, it assumes all
> characters in the file are ASCII. But when it hits non-ASCII characters
> (currency symbols), Python doesn't know how to interpret
> it. So you can give Python a hint by putting at the top of your file the
> encoding of your file:
>
> After the shebang (1st line), add the following comment:
> # coding: utf-8
>
>
> (or whatever encoding your file is saved to, I think it depends on
> your file system, usually utf-8 by default on Linux)
>
> HTH,
>
>
>
> 2011/5/28 Marilyn Davis :
>
>> Hi,
>>
>>
>> I'm still on Python 2.6 and I'm trying to work some unicode handling.
>>
>>
>> I've spent some hours on this snippet of code, trying to follow PEP
>> 0263,
>> since the error tells me to see it.  I've tried other docs too and I am
>> still clueless.
>>
>> The code works, except for the comment at the end.
>>
>>
>> I would be very grateful for some help.
>>
>>
>>
>> #!/usr/bin/env python
>> '''Unicode handling for 2.6.
>> '''
>> class Currency(float):    def __str__(self):
>>        value = self.__class__.symbol +  float.__str__(self)
>>        return value
>>
>>
>> class Yen(Currency):    symbol = unichr(165)
>>
>>
>> class Pound(Currency):    symbol = unichr(163)
>>
>>
>> def main():    y = Yen(100)
>>    print unicode(y)
>>    p = Pound(100)
>>    print unicode(p)
>>
>>
>> main()
>>
>> """
>> ¥100.0
>> £100.0
>> """
>>
>>
>>
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
>
>
> --
> Alex | twitter.com/alexconrad
>


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode help

2011-05-28 Thread Martin A. Brown

Hello there,

 : I'm still on Python 2.6 and I'm trying to work some unicode 
 : handling.
 : 
 : I've spent some hours on this snippet of code, trying to follow 
 : PEP 0263, since the error tells me to see it.  I've tried other 
 : docs too and I am still clueless.

OK, so this is PEP 0263.  http://www.python.org/dev/peps/pep-0263/

Did you miss these lines?

  To define a source code encoding, a magic comment must
  be placed into the source files either as first or second
  line in the file, such as:

Or was it the lack of an explicit example for UTF-8 in the PEP?

Try adding a single line to your script, as the second line.  That 
should make your script look like:

  #! /usr/bin/env python
  # -*- coding: utf8 -*-

You might wonder why on earth you have to do this.  The interpreter 
cannot safely assume that your editor (any arbitrary text editor) 
knows how to create/save anything other than ASCII without this 
(slightly hackish) hint.

Good luck,

-Martin

-- 
Martin A. Brown
http://linux-ip.net/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode help

2011-05-28 Thread Alexandre Conrad
When Python loads your file from your file system, it assumes all
characters in the file are ASCII. But when it hits non-ASCII
characters (currency symbols), Python doesn't know how to interpret
it. So you can give Python a hint by putting at the top of your file
the encoding of your file:

After the shebang (1st line), add the following comment:
# coding: utf-8

(or whatever encoding your file is saved to, I think it depends on
your file system, usually utf-8 by default on Linux)

HTH,


2011/5/28 Marilyn Davis :
> Hi,
>
> I'm still on Python 2.6 and I'm trying to work some unicode handling.
>
> I've spent some hours on this snippet of code, trying to follow PEP 0263,
> since the error tells me to see it.  I've tried other docs too and I am
> still clueless.
>
> The code works, except for the comment at the end.
>
> I would be very grateful for some help.
>
>
> #!/usr/bin/env python
> '''Unicode handling for 2.6.
> '''
> class Currency(float):
>    def __str__(self):
>        value = self.__class__.symbol +  float.__str__(self)
>        return value
>
> class Yen(Currency):
>    symbol = unichr(165)
>
> class Pound(Currency):
>    symbol = unichr(163)
>
> def main():
>    y = Yen(100)
>    print unicode(y)
>    p = Pound(100)
>    print unicode(p)
>
> main()
>
> """
> ¥100.0
> £100.0
> """
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Alex | twitter.com/alexconrad
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode help (COM)

2004-12-05 Thread Kent Johnson
Rene Bourgoin wrote:
Thanks for the responses. i'm a non-programmer and was learning/playing with 
some pyhton COM .
I'm trying to get my resluts from an excel spreadsheet to be saved or printed or stored as a python string. when i run this the results are in  unicode.
What problem are the unicode strings causing? If you want to convert them to normal strings you can 
use the encode method:

>>> s=u'abc'
>>> s
u'abc'
>>> s.encode('ascii')
'abc'
Now it's a plain string. If the unicode string includes characters that aren't available in the 
selected encoding you will get an error:

>>> s=u'ä'
>>> s.encode('ascii')
Traceback (most recent call last):
  File "", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 0: ordinal not in 
range(128)

You can change the handling of unknown characters by passing a second 'error' 
parameter:
>>> s.encode('ascii', 'replace')
'?'
or pick a different encoding:
>>> s.encode('utf-8')
'\xc3\xa4'
You can find a list of supported encodings in the docs for the 'codecs' module.
Kent

from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
xlApp.Visible = 0
xlApp.Workbooks.Open("c:\sheet.xls")
excelout = ()
excelout = xlApp.ActiveSheet.Range("C4:D10").Value
for item in excelout:
   print item
___
No banners. No pop-ups. No kidding.
Make My Way your home on the Web - http://www.myway.com
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor