Re: [Tutor] encoding question

2014-01-05 Thread eryksun
On Sun, Jan 5, 2014 at 5:26 PM, Steven D'Aprano wrote: > On Sun, Jan 05, 2014 at 11:02:34AM -0500, eryksun wrote: >> >> > > That surprises me. I thought XML was only valid in UTF-8? Or maybe that > was wishful thinking. JSON text SHALL be encoded in Unicode: https://tools.ietf.org/html/rfc4

Re: [Tutor] encoding question

2014-01-05 Thread Alex Kleider
On 2014-01-05 14:26, Steven D'Aprano wrote: On Sun, Jan 05, 2014 at 11:02:34AM -0500, eryksun wrote: Danny walked you through the XML. Note that he didn't decode the response. It includes an encoding on the first line: That surprises me. I thought XML was only valid in UTF-8? Or maybe t

Re: [Tutor] encoding question

2014-01-05 Thread Steven D'Aprano
On Sun, Jan 05, 2014 at 11:02:34AM -0500, eryksun wrote: > Danny walked you through the XML. Note that he didn't decode the > response. It includes an encoding on the first line: > > That surprises me. I thought XML was only valid in UTF-8? Or maybe that was wishful thinking. > tr

Re: [Tutor] encoding question

2014-01-05 Thread Alex Kleider
On 2014-01-05 08:02, eryksun wrote: On Sun, Jan 5, 2014 at 2:57 AM, Alex Kleider wrote: def ip_info(ip_address): response = urllib2.urlopen(url_format_str %\ (ip_address, )) encoding = response.headers.getparam('charset') print "'encoding' is '%s

Re: [Tutor] encoding question

2014-01-05 Thread eryksun
On Sun, Jan 5, 2014 at 2:57 AM, Alex Kleider wrote: > def ip_info(ip_address): > > response = urllib2.urlopen(url_format_str %\ >(ip_address, )) > encoding = response.headers.getparam('charset') > print "'encoding' is '%s'." % (encoding, ) > inf

Re: [Tutor] encoding question

2014-01-05 Thread Steven D'Aprano
On Sat, Jan 04, 2014 at 11:57:20PM -0800, Alex Kleider wrote: > Well, I've tried the xml approach which seems promising but still I get > an encoding related error. > Is there a bug in the xml.etree module (not very likely, me thinks) or > am I doing something wrong? I'm no expert on XML, but i

Re: [Tutor] encoding question

2014-01-05 Thread Mark Lawrence
On 05/01/2014 02:31, Alex Kleider wrote: I've been maintaining both a Python3 and a Python2.7 version. The latter has actually opened my eyes to more complexities. Specifically the need to use unicode strings rather than Python2.7's default ascii. This might help http://python-future.org/ -

Re: [Tutor] encoding question

2014-01-05 Thread spir
On 01/05/2014 08:57 AM, Alex Kleider wrote: On 2014-01-04 21:20, Danny Yoo wrote: Oh! That's unfortunate! That looks like a bug on the hostip.info side. Check with them about it. I can't get the source code to whatever is implementing the JSON response, so I can not say why the city is not

Re: [Tutor] encoding question

2014-01-05 Thread spir
On 01/05/2014 03:31 AM, Alex Kleider wrote: I've been maintaining both a Python3 and a Python2.7 version. The latter has actually opened my eyes to more complexities. Specifically the need to use unicode strings rather than Python2.7's default ascii. So-called Unicode strings are not the solut

Re: [Tutor] encoding question

2014-01-05 Thread spir
On 01/04/2014 08:26 PM, Alex Kleider wrote: Any suggestions as to a better way to handle the problem of encoding in the following context would be appreciated. The problem arose because 'Bogota' is spelt with an acute accent on the 'a'. $ cat IP_info.py3 #!/usr/bin/env python3 # -*- coding : ut

Re: [Tutor] encoding question

2014-01-05 Thread spir
On 01/05/2014 12:52 AM, Steven D'Aprano wrote: If you don't understand an exception, you have no business covering it up and hiding that it took place. Never use a bare try...except, always catch the *smallest* number of specific exception types that make sense. Better is to avoid catching except

Re: [Tutor] encoding question

2014-01-04 Thread Alex Kleider
On 2014-01-04 21:20, Danny Yoo wrote: Oh! That's unfortunate! That looks like a bug on the hostip.info side. Check with them about it. I can't get the source code to whatever is implementing the JSON response, so I can not say why the city is not being properly included there. [... XML ran

Re: [Tutor] encoding question

2014-01-04 Thread Alex Kleider
On 2014-01-04 21:20, Danny Yoo wrote: Oh! That's unfortunate! That looks like a bug on the hostip.info side. Check with them about it. I can't get the source code to whatever is implementing the JSON response, so I can not say why the city is not being properly included there. [... XML ran

Re: [Tutor] encoding question

2014-01-04 Thread Danny Yoo
> then? I'm convinced that all the extraneous structure and complexity > in XML causes the people who work with it to stop caring, the result > being something that isn't for the benefit of either humans nor > computer programs. ... I'm sorry. Sometimes I get grumpy when I haven't had a Snicker

Re: [Tutor] encoding question

2014-01-04 Thread Danny Yoo
Oh! That's unfortunate! That looks like a bug on the hostip.info side. Check with them about it. I can't get the source code to whatever is implementing the JSON response, so I can not say why the city is not being properly included there. [... XML rant about to start. I am not disintereste

Re: [Tutor] encoding question

2014-01-04 Thread eryksun
On Sat, Jan 4, 2014 at 11:16 PM, Alex Kleider wrote: > {u'city': None, u'ip': u'201.234.178.62', u'lat': u'10.4', u'country_code': > u'CO', u'country_name': u'COLOMBIA', u'lng': u'-75.2833'} > > If I use my own IP the city comes in fine so there must still be some > problem with the encoding. Rep

Re: [Tutor] encoding question

2014-01-04 Thread Alex Kleider
On 2014-01-04 18:44, Danny Yoo wrote: Hi Alex, According to: http://www.hostip.info/use.html there is a JSON-based interface. I'd recommend using that one! JSON is a format that's easy for machines to decode. The format you're parsing is primarily for humans, and who knows if that will

Re: [Tutor] encoding question

2014-01-04 Thread Dave Angel
On Sat, 04 Jan 2014 18:31:13 -0800, Alex Kleider wrote: exactly what the line # -*- coding : utf -8 -*- really indicates or more importantly, is it true, since I am using vim and I assume things are encoded as ascii? I don't know vim specifically, but I'm 99% sure it will let you specify

Re: [Tutor] encoding question

2014-01-04 Thread Danny Yoo
You were asking earlier about the line: # -*- coding : utf -8 -*- See PEP 263: http://www.python.org/dev/peps/pep-0263/ http://docs.python.org/release/2.3/whatsnew/section-encodings.html It's a line that tells Python how to interpret the bytes of your source program. It allows us t

Re: [Tutor] encoding question

2014-01-04 Thread Danny Yoo
Hi Alex, According to: http://www.hostip.info/use.html there is a JSON-based interface. I'd recommend using that one! JSON is a format that's easy for machines to decode. The format you're parsing is primarily for humans, and who knows if that will change in the future to make it easier

Re: [Tutor] encoding question

2014-01-04 Thread Alex Kleider
A heartfelt thank you to those of you that have given me much to ponder with your helpful responses. In the mean time I've rewritten my procedure using a different approach all together. I'd be interested in knowing if you think it's worth keeping or do you suggest I use your revisions to my or

Re: [Tutor] encoding question

2014-01-04 Thread Steven D'Aprano
On Sat, Jan 04, 2014 at 04:15:30PM -0800, Alex Kleider wrote: > >py> 'Bogotá'.encode('utf-8') > > I'm interested in knowing how you were able to enter the above line > (assuming you have a key board similar to mine.) I'm running Linux, and I use the KDE or Gnome character selector, depending o

Re: [Tutor] encoding question

2014-01-04 Thread Steven D'Aprano
Following my previous email... On Sat, Jan 04, 2014 at 11:26:35AM -0800, Alex Kleider wrote: > Any suggestions as to a better way to handle the problem of encoding in > the following context would be appreciated. The problem arose because > 'Bogota' is spelt with an acute accent on the 'a'. Er

Re: [Tutor] encoding question

2014-01-04 Thread eryksun
On Sat, Jan 4, 2014 at 7:15 PM, Alex Kleider wrote: >> >> py> 'Bogotá'.encode('utf-8') > > I'm interested in knowing how you were able to enter the above line > (assuming you have a key board similar to mine.) I use an international keyboard layout: https://en.wikipedia.org/wiki/QWERTY#US-Intern

Re: [Tutor] encoding question

2014-01-04 Thread Alex Kleider
On 2014-01-04 15:52, Steven D'Aprano wrote: Oh great. An exception was raised. What sort of exception? What error message did it have? Why did it happen? Nobody knows, because you throw it away. Never, never, never do this. If you don't understand an exception, you have no business covering it

Re: [Tutor] encoding question

2014-01-04 Thread Steven D'Aprano
On Sat, Jan 04, 2014 at 11:26:35AM -0800, Alex Kleider wrote: > Any suggestions as to a better way to handle the problem of encoding in > the following context would be appreciated. Python gives you lots of useful information when errors occur, but unfortunately your code throws that information

Re: [Tutor] encoding question

2014-01-04 Thread Alex Kleider
On 2014-01-04 12:01, eryksun wrote: On Sat, Jan 4, 2014 at 2:26 PM, Alex Kleider wrote: . b'\xe1' is Latin-1. Look in the response headers: url = 'http://api.hostip.info/get_html.php?ip=201.234.178.62&position=true' >>> response = urllib.request.urlopen(url) >>> response.h

Re: [Tutor] encoding question

2014-01-04 Thread eryksun
On Sat, Jan 4, 2014 at 2:26 PM, Alex Kleider wrote: > The output I get on an Ubuntu 12.4LTS system is as follows: > alex@x301:~/Python/Parse$ ./IP_info.py3 > Exception raised. > IP address is 201.234.178.62: > Country: COLOMBIA (CO); City: b'Bogot\xe1'. > Lat/Long: 10.4/-75.28

Re: [Tutor] Encoding question

2009-09-09 Thread Kent Johnson
On Wed, Sep 9, 2009 at 5:06 AM, Oleg Oltar wrote: > Hi! > > One of my tests returned following text () > > The test: > from django.test.client import Client >  c = Client() > resp = c.get("/") > resp.content > > In [25]: resp.content > Out[25]: '\r\n\r\n\r\n Strict//EN" > "http://www.w3.org/TR/xht