Hello all, I was wondering how I could print the chi-squared symbol in python. I have been looking at the Unicode docs, but I figured I would ask for assistance here while I delve into it. Thanks for any help in advance. Mark Janikas Product Engineer ESRI, Geoprocessing 380 New York St. Redlands, CA 92373 909-793-2853 (2563) mjanikas@esri.com
I have found that the python 'unicode name' escape sequence, combined with the canonical list of unicode names ( http://unicode.org/Public/ UNIDATA/NamesList.txt ), is a good way of getting the symbols you want and still keeping the python code legible. From the above list, we see that the symbol name we want is GREEK SMALL LETTER CHI, so: chi = u'\N{GREEK SMALL LETTER CHI}' will do the trick. For chi^2, use: chi2 = u'\N{GREEK SMALL LETTER CHI}\N{SUPERSCRIPT TWO}' Note that to print these characters, we usually need to encode them somehow. My terminal supports UTF-8, so the following works for me: import codecs print codecs.encode(chi2, 'utf8') giving (if your mail reader supports utf8 and mine encodes it properly...): χ² Zach Pincus Program in Biomedical Informatics and Department of Biochemistry Stanford University School of Medicine On Feb 20, 2007, at 3:56 PM, Mark Janikas wrote:
Hello all,
I was wondering how I could print the chi-squared symbol in python. I have been looking at the Unicode docs, but I figured I would ask for assistance here while I delve into it. Thanks for any help in advance.
Mark Janikas
Product Engineer
ESRI, Geoprocessing
380 New York St.
Redlands, CA 92373
909-793-2853 (2563)
mjanikas@esri.com
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Thanks for all the info. That website with all the codes is great. MJ -----Original Message----- From: numpy-discussion-bounces@scipy.org [mailto:numpy-discussion-bounces@scipy.org] On Behalf Of Zachary Pincus Sent: Tuesday, February 20, 2007 4:18 PM To: Discussion of Numerical Python Subject: Re: [Numpy-discussion] Greek Letters I have found that the python 'unicode name' escape sequence, combined with the canonical list of unicode names ( http://unicode.org/Public/ UNIDATA/NamesList.txt ), is a good way of getting the symbols you want and still keeping the python code legible. From the above list, we see that the symbol name we want is GREEK SMALL LETTER CHI, so: chi = u'\N{GREEK SMALL LETTER CHI}' will do the trick. For chi^2, use: chi2 = u'\N{GREEK SMALL LETTER CHI}\N{SUPERSCRIPT TWO}' Note that to print these characters, we usually need to encode them somehow. My terminal supports UTF-8, so the following works for me: import codecs print codecs.encode(chi2, 'utf8') giving (if your mail reader supports utf8 and mine encodes it properly...): χ² Zach Pincus Program in Biomedical Informatics and Department of Biochemistry Stanford University School of Medicine On Feb 20, 2007, at 3:56 PM, Mark Janikas wrote:
Hello all,
I was wondering how I could print the chi-squared symbol in python. I have been looking at the Unicode docs, but I figured I would ask for assistance here while I delve into it. Thanks for any help in advance.
Mark Janikas
Product Engineer
ESRI, Geoprocessing
380 New York St.
Redlands, CA 92373
909-793-2853 (2563)
mjanikas@esri.com
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Mark Janikas wrote:
Hello all,
I was wondering how I could print the chi-squared symbol in python. I have been looking at the Unicode docs, but I figured I would ask for assistance here while I delve into it. Thanks for any help in advance.
Print it where? To the terminal (which one?)? In HTML? With some GUI? Assuming that you have a Unicode-capable terminal, you can find out the encoding it uses by looking at sys.stdout.encoding. Encode your Unicode string with that encoding, and print it. E.g., I use iTerm on OS X and set it to use UTF-8 as the encoding: In [5]: import sys In [6]: sys.stdout.encoding Out[6]: 'UTF-8' In [7]: print u'\u03a7\u00b2'.encode(sys.stdout.encoding) Χ² -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
Thanks Robert.... but alas, I get.....
import sys sys.stdout.encoding 'cp437' print u'\u03a7\u00b2'.encode(sys.stdout.encoding) Traceback (most recent call last): File "<stdin>", line 1, in ? File "C:\Python24\lib\encodings\cp437.py", line 18, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character u'\u03a7' in position 0: character maps to <undefined>
Ill keep at it.... please let me know if you have any solutions.... Thanks again, MJ -----Original Message----- From: numpy-discussion-bounces@scipy.org [mailto:numpy-discussion-bounces@scipy.org] On Behalf Of Robert Kern Sent: Tuesday, February 20, 2007 4:20 PM To: Discussion of Numerical Python Subject: Re: [Numpy-discussion] Greek Letters Mark Janikas wrote:
Hello all,
I was wondering how I could print the chi-squared symbol in python. I have been looking at the Unicode docs, but I figured I would ask for assistance here while I delve into it. Thanks for any help in advance.
Print it where? To the terminal (which one?)? In HTML? With some GUI? Assuming that you have a Unicode-capable terminal, you can find out the encoding it uses by looking at sys.stdout.encoding. Encode your Unicode string with that encoding, and print it. E.g., I use iTerm on OS X and set it to use UTF-8 as the encoding: In [5]: import sys In [6]: sys.stdout.encoding Out[6]: 'UTF-8' In [7]: print u'\u03a7\u00b2'.encode(sys.stdout.encoding) Χ² -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Oh. I am using CygWin, and the website I just went to: http://www.cygwin.com/faq/faq_3.html stated that: " The short answer is that Cygwin is not Unicode-aware" Not sure if this is going to apply to python in general, but I suspect it will. Ugh, I dislike Windows a lot, but it pays the bills. The interesting thing to note is that the print out to gui interface is 'UTF-8' so it works. It just wont work on my terminal where I do all of my testing. I might just have to put a try statement in and put a "chi-square" in the except. MJ -----Original Message----- From: numpy-discussion-bounces@scipy.org [mailto:numpy-discussion-bounces@scipy.org] On Behalf Of Mark Janikas Sent: Tuesday, February 20, 2007 5:16 PM To: Discussion of Numerical Python Subject: Re: [Numpy-discussion] Greek Letters Thanks Robert.... but alas, I get.....
import sys sys.stdout.encoding 'cp437' print u'\u03a7\u00b2'.encode(sys.stdout.encoding) Traceback (most recent call last): File "<stdin>", line 1, in ? File "C:\Python24\lib\encodings\cp437.py", line 18, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character u'\u03a7' in position 0: character maps to <undefined>
Ill keep at it.... please let me know if you have any solutions.... Thanks again, MJ -----Original Message----- From: numpy-discussion-bounces@scipy.org [mailto:numpy-discussion-bounces@scipy.org] On Behalf Of Robert Kern Sent: Tuesday, February 20, 2007 4:20 PM To: Discussion of Numerical Python Subject: Re: [Numpy-discussion] Greek Letters Mark Janikas wrote:
Hello all,
I was wondering how I could print the chi-squared symbol in python. I have been looking at the Unicode docs, but I figured I would ask for assistance here while I delve into it. Thanks for any help in advance.
Print it where? To the terminal (which one?)? In HTML? With some GUI? Assuming that you have a Unicode-capable terminal, you can find out the encoding it uses by looking at sys.stdout.encoding. Encode your Unicode string with that encoding, and print it. E.g., I use iTerm on OS X and set it to use UTF-8 as the encoding: In [5]: import sys In [6]: sys.stdout.encoding Out[6]: 'UTF-8' In [7]: print u'\u03a7\u00b2'.encode(sys.stdout.encoding) Χ² -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
On Tue, Feb 20, 2007 at 05:29:25PM -0800, Mark Janikas wrote:
Oh. I am using CygWin, and the website I just went to:
http://www.cygwin.com/faq/faq_3.html
stated that: " The short answer is that Cygwin is not Unicode-aware"
Not sure if this is going to apply to python in general, but I suspect it will. Ugh, I dislike Windows a lot, but it pays the bills.
Actually you pay Bill :) Maybe try the free vmplayer with a linux session? Cheers Stéfan
Mark Janikas wrote:
Thanks Robert.... but alas, I get.....
import sys sys.stdout.encoding 'cp437' print u'\u03a7\u00b2'.encode(sys.stdout.encoding) Traceback (most recent call last): File "<stdin>", line 1, in ? File "C:\Python24\lib\encodings\cp437.py", line 18, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character u'\u03a7' in position 0: character maps to <undefined>
Ill keep at it.... please let me know if you have any solutions....
Yup, CP437 doesn't support the Χ character. You will have to use a terminal that can accept a full-Unicode encoding like UTF-8 and a font that has the relevant characters. Most of the modern terminal emulators for Linux et al. are capable of UTF-8. On Windows, you may be out of luck. I don't know of any fully-Unicode-capable terminal. Google tells me that cmd.exe takes a /U argument that should put it in Unicode mode, but that doesn't change anything for me. I also ran into a case where sys.stdout.encoding is None, so my solution is not as robust as I thought. If you use something else for your interpreter shell like IDLE, it is possible that it supports Unicode. sys.stdout.encoding may not be set appropriately, though. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
Great info. The last part about the sys.stdout not working everywhere is a bit troubling. But For now, I am just gonna add a try statement as stated in the previous message. Thanks again, MJ PS... The software company I work for has a version that runs on Unix/Linux so I am awaiting a new laptop with which to employ Linux! I cant wait to get back to my roots. Debian and Fluxbox.... could there be a better working environment? -----Original Message----- From: numpy-discussion-bounces@scipy.org [mailto:numpy-discussion-bounces@scipy.org] On Behalf Of Robert Kern Sent: Tuesday, February 20, 2007 5:36 PM To: Discussion of Numerical Python Subject: Re: [Numpy-discussion] Greek Letters Mark Janikas wrote:
Thanks Robert.... but alas, I get.....
import sys sys.stdout.encoding 'cp437' print u'\u03a7\u00b2'.encode(sys.stdout.encoding) Traceback (most recent call last): File "<stdin>", line 1, in ? File "C:\Python24\lib\encodings\cp437.py", line 18, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character u'\u03a7' in position 0: character maps to <undefined>
Ill keep at it.... please let me know if you have any solutions....
Yup, CP437 doesn't support the Χ character. You will have to use a terminal that can accept a full-Unicode encoding like UTF-8 and a font that has the relevant characters. Most of the modern terminal emulators for Linux et al. are capable of UTF-8. On Windows, you may be out of luck. I don't know of any fully-Unicode-capable terminal. Google tells me that cmd.exe takes a /U argument that should put it in Unicode mode, but that doesn't change anything for me. I also ran into a case where sys.stdout.encoding is None, so my solution is not as robust as I thought. If you use something else for your interpreter shell like IDLE, it is possible that it supports Unicode. sys.stdout.encoding may not be set appropriately, though. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Robert Kern wrote:
On Windows, you may be out of luck. I don't know of any fully-Unicode-capable terminal. The lack of a decent console application is one of the most problematic issues I face whenever attempting to do serious programming in Windows. I wish I knew of a better terminal program. Here's one that seems like it might work, but I haven't tried it yet: http://software.jessies.org/terminator
Andrew Straw wrote:
Here's one that seems like it might work, but I haven't tried it yet: http://software.jessies.org/terminator
Now if only there was a decent terminal emulator for Windows that didn't use cygwin... -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
participants (6)
-
Andrew Straw -
Chris Barker -
Mark Janikas -
Robert Kern -
Stefan van der Walt -
Zachary Pincus