In python I'm having a unicode escape sequence that is originally a part of chinease text. I'm trying to display it properly (to convert into Unicode string). Searching SO I've tried several proposed ways but none of them work. Here's what I got:  import re import codecs import urllib  ESCAPE_SEQUENCE_RE = re.compile(r'''     ( \\U........      # 8-digit hex escapes     | \\u....          # 4-digit hex escapes     | \\x..            # 2-digit hex escapes     | \\[0-7]{1,3}     # Octal escapes     | \\N\{[^}]+\}     # Unicode characters by name     | \\[\\'"abfnrtv]  # Single-character escapes     )''', re.UNICODE | re.VERBOSE)  def decode_escapes(s):     def decode_match(match):         return codecs.decode(match.group(0), 'unicode-escape')      return ESCAPE_SEQUENCE_RE.sub(decode_match, s)   print(decode_escapes('\u6240\u8BF7\u6C42\u7684\u8FD4\u7A0B\u65E5'))   Trying to execute this code will fail with this error:  Traceback (most recent call last):   File "Test.py", line 21, in &lt;module&gt;     print(decode_escapes('\u6240\u8BF7\u6C42\u7684\u8FD4\u7A0B\u65E5'))   File "C:\Python27\lib\encodings\cp437.py", line 12, in encode     return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-6: character maps to &lt;undefined&gt;   Finally it should be like this:  所请求的返程日   Could you suggest what I can do to see correct string?
When I try to print a Unicode string in a Windows console, I get an error . UnicodeEncodeError: 'charmap' codec can't encode character .... I assume this is because the Windows console does not accept Unicode-only characters. What's the best way around this? Is there any way I can make Python automatically print a ? instead of failing in this situation? Edit:  I'm using Python 2.5.  Note: @LasseV.Karlsen answer with the checkmark is sort of outdated (from 2008). Please use the solutions/answers/suggestions below with care!!  is more relevant as of today (6 Jan 2016).
Applications of complex numbers to solve non-complex problems Recently I asked a question regarding the diophantine equation $x^2+y^2=z^n$ for $x, y, z, n \in \mathbb{N}$, which to my surprise was answered with the help complex numbers. I find it fascinating that for a question which only concerns integers, and whose answers can only be integers, such an elegant solution comes from the seemingly unrelated complex numbers - looking only at the question and solution one would never suspect that complex numbers were lurking behind the curtain!  Can anyone give some more examples where a problem which seems to deal entirely with real numbers can be solved using complex numbers behind the scenes? One other example which springs to mind for me is solving a homogeneous second order differential equation whose coefficients form a quadratic with complex roots, which in some cases gives real solutions for real coefficients but requires complex arithmetic to calculate.  (If anyone is interested, the original question I asked can be found here: )  EDIT:  I just wanted to thank everyone for all the great answers! I'm working my way through all of them, although some are beyond me for now! When is it best to write "can't" versus writing "cannot"? Are they interchangeable in every situation? What does 2>/dev/null mean? I would like a brief explanation of the following command line:  grep -i 'abc' content 2&gt;/dev/null Weird $3^n$ in an identity to be combinatorially proved Let $a,b$ be positive integers.  When $$k = \frac{a^2 + b^2}{ab+1}$$ is an integer, it is a square.    Proof 1: (Ngô Bảo Châu): Rearrange to get $a^2-akb+b^2-k=0$, as a quadratic in $a$ this has two values: $a$ and $kb - a = (b^2-k)/a$. (The second root is determined in two different ways from the expansion $(x-r_1)(x-r_2) = x^2 - (r_1 + r_2)x + r_1 r_2$.)  Now suppose we have $a,b$ such that $k$ is an integer but not a square, by the investigation about roots we have that the second root is a nonzero integer since $k,b,a$ are integers and $k \not = b^2$, futhermore it is positive which is easily seen from its defining equation.  WLOG assume $a \ge b$ so that the second root is strictly smaller than $a$. This leads to a decent, replacing $a$ with the second root.    Proof 2 (Don Zagier): Apply reduction theory (specifically, Sätze 1 and 2 of Section 13 of my book on quadratic fields) to the quadratic form $x^2 + kxy + y^2$, which is the unique reduced quadratic form in its equivalence class.    Note that Proof 2 is pretty much the same as Proof 1 when written out in explicit detail, but I could not read Zagier's book because I cannot read German.  I would like to know more approaches to this and other alternative proofs of this result if possible! Thanks in advance.  I would also be interested in related problems (especially easier ones of a similar nature) and texts which cover the reduction theory in English. Why are the United States often referred to as America? How can I make something that looks like this?  --------   Text   ---------  where the horizontal rules extend to the margin on either side? .NET String.Format() to add commas in thousands place for a number I want to add a comma in the thousands place for a number.     String.Format()? 80's pop song with lyric "It's the story of my life if you wanna know" I'm trying to identify a song by a female 80's pop group.   Here are the lyrics I remember:     It's the story of my life if you want to know   a play where everything is meant to be   you're the story of my life and I can't let you go Prove using induction that from a set of $n+1$ numbers from $\{1,2,..2n\}$, at least one number will evenly divide another. Given a set of $n+1$ numbers out of the first $2n$ natural numbers, $\{1,2,\ldots,2n\}$, prove that there are two numbers in the set, one of which divides the other.  I can't tell if I'm reducing the problem in the right way.  Would it be something like:  Induction Hypothesis: A set of $n+1$ numbers from the first $2n$ natural numbers contains two numbers, one of which evenly divides the other.  Base Case: $n=1$, $\{1,2\}$, 2 is divisible by 1.  Consider a set of $n+2$ numbers, $A$, from the first $2n+2$ natural numbers. There are three cases:   Each number in $A$ is less than or equal to $2n$.  The proof follows from the induction hypothesis. One number in $A$ is greater than $2n$.  The remaining set of $n+1$ numbers must each be less than or equal to $2n$.  The proof follows from the induction hypothesis. $A$ contains both $2n+1$ and $2n+2$. I'm not sure how to solve this case.   I'm working through Udi Manber's Introduction To Algorithms: A Creative Approach for personal development, so in keeping with the spirit of the book, I'm trying to use induction.  Without induction, the question is identical to I am trying to calculate the sum of this infinite series after having read the series chapter of my textbook: $$\sum_{n=1}^{\infty}\frac{1}{4n^2-1}$$  my steps:   $$\sum_{n=1}^{\infty}\frac{1}{4n^2-1}=\sum_{n=1}^{\infty}\frac{2}{4n^2-1}-\sum_{n=1}^{\infty}\frac{1}{4n^2-1}=..help..=sum$$  I am lacking some important properties, I feel I am coming to the right step and cannot spit that out.. How to evaluate $ \lim_{(x,y)\to(0,0)}\frac{y^2\sin^2x}{x^4+y^4} $ jQuery selector regular expressions How to use grep on column? I want to apply grep on particular column in column command.  myfile.txt  3,ST,ST01,3,3,856 3,ST,ST02,4,9,0234 6,N1,N101,2,3,ST 6,N1,N102,1,60,Comcast 6,N1,N103,1,2,92   My Command:  column -s, -t &lt; myfile.txt | grep -w "ST"   Here I want to only grep the pattern ST in 2nd column. How to do this ?  Expected Result:  3       ST            ST01              3            3            856 3       ST            ST02              4            9            0234 How to pronounce "Stannis's"? I know that the &quot;s&quot; becomes &quot;z&quot; when it comes after a voiced sound. And it is pronounced as &quot;s&quot; when it comes after a voiceless sound. I also know rules about possessives but I don't know how natives pronounce a word when it ends in S and &quot;apostrophe S&quot; is added to it. For example: &quot;Stannis's army&quot;. Do you add another syllable to it? Like Stannisiz? or just Stannis and ignore the possessive S? I really have difficulty in pronouncing words that end in S and possessive S is attached to it. How to urlencode data for curl command? I am trying to write a bash script for testing that takes a parameter and sends it through curl to web site.  I need to url encode the value to make sure that special characters are processed properly.  What is the best way to do this?    Here is my basic script so far:  #!/bin/bash host=${1:?'bad host'} value=$2 shift shift curl -v -d "param=${value}" http://${host}/somepath $@ How do I control how Emacs makes backup files? Emacs puts backup files named foo~ everywhere and I don't like having to remember to delete them.  Also, if I edit a file that has a hard link somewhere else in the file system, the hard link points to the backup when I'm done editing, and that's confusing and awful.  How can I either eliminate these backup files, or have them go somewhere other than the same directory? Does commutativity imply Associativity? Does commutativity imply associativity? I'm asking this because I was trying to think of structures that are commutative but non-associative but couldn't come up with any. Are there any such examples?  NOTE: I wasn't sure how to tag this so feel free to retag it. Suppose $n$ is a non negative integer $\geq 4$ and $\sigma\in S_n$ a permutation. Conjugacy classes in $S_n$ are completley determined by the cycle structure of $\sigma$. If we let the alternating group $A_n$.act on $S_n$ by conjugation, the orbits coincide with the conjugacy classes in $S_n$ except for those (even) permutations whose cycle structure is composed of even cycles (which corresponds to cycles of uneven length) of distinct order (fixed points are treated as cycles of length $1$.) See   For instance a $3$-cycle in $A_4$ has cycle structure $[3,1]$ which fits the bill, and a $5$-cycle in $A_5$ or $A_6$ works too, since the cycle structure is $[5]$ and $[5,1]$ respectively. On the other hand, a $3$-cycle in $A_5$ will does not satisfy the criteria as it has cycle structure $[3,1,1]$ with two fixed points.  In this case, the conjugacy class of $\sigma$ inside $S_n$ splits in $2$ orbits of the conjugation action of $A_n$, and if $\tau$ is any odd permutation, for instance $(12)$, then we have $$\mathrm{Conj}_{S_n}(\sigma)=\mathcal{O}(\sigma)\coprod\mathcal{O}(\tau\sigma\tau^{-1})$$  My question is How can we differentiate between the two orbits? Does it have geometric meaning?    I can see that this information can be useful when looking at representations of the alternating groups, since there are as many irreducible representations as conjugacy classes. As for some geometric interpretation, I ask because there is one for the three cycles in $A_4$. If $S_4$ is understood as the group of symmetries of a regular tetrahedron, then $A_4$ is the subgroup of direct symmetries, and a $3$-cycle corresponds to a rotation of angle $\frac{2\pi}{3}$ or $\frac{4\pi}{3}$ with axis passing through one of the vertices. Conjugacy classes inside $A_n$ preserve the angle, and that's how we can tell them apart. In this case the computations are so simple we don't really need this picture, but I think it is a nice way to understand the fact $3$-cycles in are not all conjugate inside $A_4$... How to access a clicked URL in a URL handler application created in Automator I'm currently trying to modify the default URL handling behavior in OS X. I would like, when clicking on any link outside of a web browser, to copy this URL to the pasteboard instead of opening it in the default web browser – for instance, clicking a link in a Skype conversation should copy this URL to the OS X pasteboard instead of opening it in Firefox (which is currently my default web browser).  I have thought about solutions, but I’m currently unable to implement one. If I can create an application that puts URLs into the pasteboard that are sent to it, and can set this application as the default web browser, I would be able to achieve my goal.  I created an application with Automator that executes the shell command pbcopy and have set this application as my default web browser, but I didn’t get the expected result. I did some testing and I can confirm that the application runs on clicking on a link, however, I cannot get the URL passed to the application / shell script. I tried both stdin or a passed argument, but without success.  Is there a way for an application created with Automator to retrieve a clicked URL and send it to pbcopy, when that application is set as a (default) handler for URLs?  Alternatively, is there a better way to create a fake web browser that takes the input and puts it in the pasteboard than Automator? Or is there any other way that I can achieve my goal without a custom application / script?