I keep getting this error message when i try to run a program in Python
Dear all I am totally new in learning Python and i am learning from a beginner's book called automate the boring stuff with Python and i downloaded the Python 64 bit version for Windows and i started writing some examples from the book. At the end whenever i try to run the program i keep getting an error message and it shows that the error is in the beginning in the version of Python. (See a pic bellow) Can someone help me and tell me what to do and why is this error showing up??? Thanks Dan -- https://mail.python.org/mailman/listinfo/python-list
Re: I keep getting this error message when i try to run a program in Python
i am sorry but i can not find a way to attach an image here so you can see what i mean. Please someone tell me how to add an image here. Thanks Dan -- https://mail.python.org/mailman/listinfo/python-list
Re: I keep getting this error message when i try to run a program in Python
On Thu, Mar 22, 2018 at 2:37 PM, Damjan Stojanovski wrote: > i am sorry but i can not find a way to attach an image here so you can see > what i mean. Please someone tell me how to add an image here. You can't. Copy/paste the error. And please include the post you are replying to. -- https://mail.python.org/mailman/listinfo/python-list
Re: I keep getting this error message when i try to run a program in Python
On 3/22/2018 2:37 PM, Damjan Stojanovski wrote: i am sorry but i can not find a way to attach an image here so you can see what i mean. Please someone tell me how to add an image here. You cannot and there is no need. List is text only, no attachments. Reduce code to the minimum needed to get error. Copy and paste both code and trackback. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: I keep getting this error message when i try to run a program in Python
On 2018-03-22 19:37, Damjan Stojanovski wrote: > i am sorry but i can not find a way to attach an image here so you can see > what i mean. Please someone tell me how to add an image here. > This is a text only list. -- https://mail.python.org/mailman/listinfo/python-list
Putting Unicode characters in JSON
I have some mailing information in a Mysql database that has
characters from various other countries. The table says that
it's using latin-1 encoding. I want to send this data out
as JSON.
So I'm just taking each datum and doing 'name'.decode('latin-1')
and adding the resulting Unicode value right into my JSON structure
before doing .dumps() on it. This seems to work, and I can consume
the JSON with another program and when I print values, they look nice
with the special characters and all.
I was reading though, that JSON files must be encoded with UTF-8. So
should I be doing string.decode('latin-1').encode('utf-8')? Or does
the json module do that for me when I give it a unicode object?
Thanks
--
https://mail.python.org/mailman/listinfo/python-list
Re: Putting Unicode characters in JSON
On Fri, Mar 23, 2018 at 6:46 AM, Tobiah wrote:
> I have some mailing information in a Mysql database that has
> characters from various other countries. The table says that
> it's using latin-1 encoding. I want to send this data out
> as JSON.
>
> So I'm just taking each datum and doing 'name'.decode('latin-1')
> and adding the resulting Unicode value right into my JSON structure
> before doing .dumps() on it. This seems to work, and I can consume
> the JSON with another program and when I print values, they look nice
> with the special characters and all.
>
> I was reading though, that JSON files must be encoded with UTF-8. So
> should I be doing string.decode('latin-1').encode('utf-8')? Or does
> the json module do that for me when I give it a unicode object?
Reconfigure your MySQL database to use UTF-8. There is no reason to
use Latin-1 in the database.
If that isn't an option, make sure your JSON files are pure ASCII,
which is the common subset of UTF-8 and Latin-1.
ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Putting Unicode characters in JSON
On 03/22/2018 01:09 PM, Chris Angelico wrote:
On Fri, Mar 23, 2018 at 6:46 AM, Tobiah wrote:
I have some mailing information in a Mysql database that has
characters from various other countries. The table says that
it's using latin-1 encoding. I want to send this data out
as JSON.
So I'm just taking each datum and doing 'name'.decode('latin-1')
and adding the resulting Unicode value right into my JSON structure
before doing .dumps() on it. This seems to work, and I can consume
the JSON with another program and when I print values, they look nice
with the special characters and all.
I was reading though, that JSON files must be encoded with UTF-8. So
should I be doing string.decode('latin-1').encode('utf-8')? Or does
the json module do that for me when I give it a unicode object?
Reconfigure your MySQL database to use UTF-8. There is no reason to
use Latin-1 in the database.
If that isn't an option, make sure your JSON files are pure ASCII,
which is the common subset of UTF-8 and Latin-1.
ChrisA
It works the way I'm doing it. I checked and it turns out that
whether I do datum.decode('latin-1') or datum.decode('latin-1').encode('utf8')
I get identical JSON files after .dumps().
--
https://mail.python.org/mailman/listinfo/python-list
PyQt4 QWebView cant load google maps markers
I want to create a simple python app using pyqt,QWebView and google maps with markers. The problem is that,the markers does not load inside the QWebView, as you can see they load just fine in the browser. here the simple code : import sys from PyQt4.QtCore import * from PyQt4.QtGui import * from PyQt4.QtWebKit import * import os app = QApplication(sys.argv) web_view= dialog.findChild(QWebView,"webView") google='https://www.google.gr/maps/place/Granite+Mountain+Hotshots+Memorial+State+Park/@34.1749572,-112.850308,12.78z/data=!4m13!1m7!3m6!1s0x872b08ebcb4c186b:0x423927b17fc1cd71!2zzpHPgc65zrbPjM69zrEsIM6Xzr3Pic68zq3Ovc61z4IgzqDOv867zrnPhM61zq_Otc-C!3b1!8m2!3d34.0489281!4d-111.0937311!3m4!1s0x80d330577faee965:0x66d75aef24890ae1!8m2!3d34.2032843!4d-112.7746582?hl=el' web_view2.load(QUrl(google)) web_view2.show() sys.exit(app.exec_()) error message : right click and select Inspect, then go to console and observe the messages console I take this error : TypeError: 'undefined' is not a function (evaluating 'this.D.bind(this)') marker.js:58 Any idea how to can fix that ? I need to add more settings to QWebView to work google maps with markers ? -- https://mail.python.org/mailman/listinfo/python-list
Re: Putting Unicode characters in JSON
On Fri, 23 Mar 2018 07:09:50 +1100, Chris Angelico wrote:
>> I was reading though, that JSON files must be encoded with UTF-8. So
>> should I be doing string.decode('latin-1').encode('utf-8')? Or does
>> the json module do that for me when I give it a unicode object?
>
> Reconfigure your MySQL database to use UTF-8. There is no reason to use
> Latin-1 in the database.
You don't know that. You don't know what technical, compatibility, policy
or historical constraints are on the database.
> If that isn't an option, make sure your JSON files are pure ASCII, which
> is the common subset of UTF-8 and Latin-1.
And that's utterly unnecessary, since any character which can be stored
in the Latin-1 MySQL database can be stored in the Unicode JSON.
--
Steven
--
https://mail.python.org/mailman/listinfo/python-list
Re: Putting Unicode characters in JSON
On Fri, Mar 23, 2018 at 10:47 AM, Steven D'Aprano
wrote:
> On Fri, 23 Mar 2018 07:09:50 +1100, Chris Angelico wrote:
>
>>> I was reading though, that JSON files must be encoded with UTF-8. So
>>> should I be doing string.decode('latin-1').encode('utf-8')? Or does
>>> the json module do that for me when I give it a unicode object?
>>
>> Reconfigure your MySQL database to use UTF-8. There is no reason to use
>> Latin-1 in the database.
>
> You don't know that. You don't know what technical, compatibility, policy
> or historical constraints are on the database.
Okay. Give me a good reason for the database itself to be locked to
Latin-1. Make sure you explain how potentially saving the occasional
byte of storage (compared to UTF-8) justifies limiting the available
character set to the ones that happen to be in Latin-1, yet it's
essential to NOT limit the character set to ASCII.
>> If that isn't an option, make sure your JSON files are pure ASCII, which
>> is the common subset of UTF-8 and Latin-1.
>
> And that's utterly unnecessary, since any character which can be stored
> in the Latin-1 MySQL database can be stored in the Unicode JSON.
>
Irrelevant; if you fetch eight-bit data out of the database, it isn't
going to be a valid JSON file unless (1) it's really ASCII, like I
suggest; (2) you re-encode it to UTF-8; or (3) it was actually UTF-8
all along, despite being declared as Latin-1.
Restricting JSON to ASCII is a very easy and common thing to do. It
just means that every non-ASCII character gets represented as a \u
escape sequence. In Python's JSON encoder, that's the ensure_ascii
parameter. Utterly unnecessary? How about standards-compliant and
entirely effective, unlike the re-encoding that means that the
database-stored blob is invalid JSON and must be re-encoded again on
the way out?
ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Putting Unicode characters in JSON
Chris Angelico writes: > On Fri, Mar 23, 2018 at 10:47 AM, Steven D'Aprano > wrote: > > On Fri, 23 Mar 2018 07:09:50 +1100, Chris Angelico wrote: > >> Reconfigure your MySQL database to use UTF-8. There is no reason to > >> use Latin-1 in the database. > > > > You don't know that. You don't know what technical, compatibility, > > policy or historical constraints are on the database. > > Okay. Give me a good reason for the database itself to be locked to > Latin-1. That's a loaded question. If the database maintainers have a technical, compatibility, policy, or historical reason that constrains an existing database to a specific character set, then that is a good reason for them. Is it a good reason to create a new, independent database using Latin-1 character set? No. But that's implicit in the fact it's an existing constraint, because it's an existing database. Is it a reason likely to convince you? Or convince me? No. But that's irrelevant because they don't have any need to convince us of the goodness of their reason to keep that encoding :-) -- \ “Instead of having ‘answers’ on a math test, they should just | `\ call them ‘impressions’, and if you got a different | _o__) ‘impression’, so what, can't we all be brothers?” —Jack Handey | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: Putting Unicode characters in JSON
On Fri, Mar 23, 2018 at 11:25 AM, Ben Finney wrote: > Chris Angelico writes: > >> On Fri, Mar 23, 2018 at 10:47 AM, Steven D'Aprano >> wrote: >> > On Fri, 23 Mar 2018 07:09:50 +1100, Chris Angelico wrote: >> >> Reconfigure your MySQL database to use UTF-8. There is no reason to >> >> use Latin-1 in the database. >> > >> > You don't know that. You don't know what technical, compatibility, >> > policy or historical constraints are on the database. >> >> Okay. Give me a good reason for the database itself to be locked to >> Latin-1. > > That's a loaded question. If the database maintainers have a technical, > compatibility, policy, or historical reason that constrains an existing > database to a specific character set, then that is a good reason for > them. > > Is it a good reason to create a new, independent database using Latin-1 > character set? No. But that's implicit in the fact it's an existing > constraint, because it's an existing database. > > Is it a reason likely to convince you? Or convince me? No. But that's > irrelevant because they don't have any need to convince us of the > goodness of their reason to keep that encoding :-) I happen to know that many deployments of MySQL have Latin-1 as the default character set. Is *that* a good enough reason for you? Because it isn't for me. There is NOT always a good reason for a suboptimal configuration. Thus I stand by my recommendation to reconfigure the database as first option, with other options *following* it in my original email. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Putting Unicode characters in JSON
Chris Angelico writes: > There is NOT always a good reason for a suboptimal configuration. True. Did anyone claim otherwise? What I saw Steven responding to was your claim that there is *never* a good reason to do it. To refute that, it's sufficient to show that good reason can exist in some cases. That's entirely compatible with a good reason not existing in other cases. -- \ “Education is learning what you didn't even know you didn't | `\ know.” —Daniel J. Boorstin, historian, 1914–2004 | _o__) | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: Putting Unicode characters in JSON
On 22/03/18 20:46, Tobiah wrote:
> I was reading though, that JSON files must be encoded with UTF-8. So
> should I be doing string.decode('latin-1').encode('utf-8')? Or does
> the json module do that for me when I give it a unicode object?
Definitely not. In fact, that won't even work.
>>> import json
>>> s = 'déjà vu'.encode('latin1')
>>> s
b'd\xe9j\xe0 vu'
>>> json.dumps(s.decode('latin1').encode('utf8'))
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python3.6/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'bytes' is not JSON serializable
>>>
You should make sure that either the file you're writing to is opened as
UTF-8 text, or the ensure_ascii parameter of dumps() or dump() is set to
True (the default) – and then write the data in ASCII or any
ASCII-compatible encoding (e.g. UTF-8).
Basically, the default behaviour of the json module means you don't
really have to worry about encodings at all once your original data is
in unicode strings.
-- Thomas
--
https://mail.python.org/mailman/listinfo/python-list
Re: Putting Unicode characters in JSON
On Fri, Mar 23, 2018 at 11:39 AM, Ben Finney wrote:
> Chris Angelico writes:
>
>> There is NOT always a good reason for a suboptimal configuration.
>
> True. Did anyone claim otherwise?
>
> What I saw Steven responding to was your claim that there is *never* a
> good reason to do it.
>
> To refute that, it's sufficient to show that good reason can exist in
> some cases. That's entirely compatible with a good reason not existing
> in other cases.
>
I'll concede that sometimes it's a lot of effort to fix misconfigured
databases. However, it's a form of technical debt, and when you start
investigating problems ("this isn't working because X is incompatible
with Y"), that's an indication that it's starting to cost you to
*maintain* the misconfiguration. Hence it's more likely to be worth
just paying off your technical debt and moving forward.
ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Putting Unicode characters in JSON
On Fri, 23 Mar 2018 11:08:56 +1100, Chris Angelico wrote:
> On Fri, Mar 23, 2018 at 10:47 AM, Steven D'Aprano
> wrote:
>> On Fri, 23 Mar 2018 07:09:50 +1100, Chris Angelico wrote:
>>
I was reading though, that JSON files must be encoded with UTF-8. So
should I be doing string.decode('latin-1').encode('utf-8')? Or does
the json module do that for me when I give it a unicode object?
>>>
>>> Reconfigure your MySQL database to use UTF-8. There is no reason to
>>> use Latin-1 in the database.
>>
>> You don't know that. You don't know what technical, compatibility,
>> policy or historical constraints are on the database.
>
> Okay. Give me a good reason for the database itself to be locked to
> Latin-1. Make sure you explain how potentially saving the occasional
> byte of storage (compared to UTF-8) justifies limiting the available
> character set to the ones that happen to be in Latin-1, yet it's
> essential to NOT limit the character set to ASCII.
I'll better than that, I'll give multiple good reasons to use Latin-1.
It's company policy to only use Latin-1, because the CEO was once
employed by the Unicode Consortium, and fired in disgrace after
embezzling funds, and ever since then he has refused to use Unicode.
Compatibility with other databases, systems or tools that require Latin-1.
The database has to send information to embedded devices that don't
include a full Unicode implementation, but do support Latin-1.
The data doesn't actually represent text, but Python 2 style byte-
strings, and Latin-1 is just a convenient, easy way to get that that
ensures ASCII bytes look like ASCII characters.
>>> If that isn't an option, make sure your JSON files are pure ASCII,
>>> which is the common subset of UTF-8 and Latin-1.
>>
>> And that's utterly unnecessary, since any character which can be stored
>> in the Latin-1 MySQL database can be stored in the Unicode JSON.
>>
>>
> Irrelevant; if you fetch eight-bit data out of the database, it isn't
> going to be a valid JSON file unless (1) it's really ASCII, like I
> suggest; (2) you re-encode it to UTF-8; or (3) it was actually UTF-8 all
> along, despite being declared as Latin-1.
As Tobiah pointed out in his question, he's fetching the data from the
database, calling decode('latin-1'), and placing the resulting Unicode
string into the JSON. There's no need to explicitly encode the Unicode
string to a UTF-8 byte string, in fact it is the wrong thing to do since
JSON doesn't support it:
# The right way is to use a Unicode string.
py> json.dumps("Hello ü")
'"Hello \\u00fc"'
# The wrong way is to encode to UTF-8 first.
py> json.dumps("Hello ü".encode('utf-8'))
Traceback (most recent call last):
...
TypeError: b'Hello \xc3\xbc' is not JSON serializable
(Results in Python 3 -- Python 2 may be doing something shonky.)
--
Steve
--
https://mail.python.org/mailman/listinfo/python-list
Re: Putting Unicode characters in JSON
On Fri, Mar 23, 2018 at 11:39 AM, Steven D'Aprano wrote: > On Fri, 23 Mar 2018 11:08:56 +1100, Chris Angelico wrote: >> Okay. Give me a good reason for the database itself to be locked to >> Latin-1. Make sure you explain how potentially saving the occasional >> byte of storage (compared to UTF-8) justifies limiting the available >> character set to the ones that happen to be in Latin-1, yet it's >> essential to NOT limit the character set to ASCII. > > I'll better than that, I'll give multiple good reasons to use Latin-1. > > It's company policy to only use Latin-1, because the CEO was once > employed by the Unicode Consortium, and fired in disgrace after > embezzling funds, and ever since then he has refused to use Unicode. You clearly can't afford to quit your job, so I won't mention that possibility. (Oops too late.) But a CEO is not God, and you *can* either dispute or subvert stupid orders. I don't consider this a *good* reason. Maybe a reason, but not a good one. > Compatibility with other databases, systems or tools that require Latin-1. Change them one at a time. When you have to pass data to something that has to receive Latin-1, you encode it to Latin-1. The database can still store UTF-8. Leaving it at Latin-1 is not "good reason for using Latin-1", so much as "we haven't gotten around to changing it yet". > The database has to send information to embedded devices that don't > include a full Unicode implementation, but do support Latin-1. Okay, that's a valid reason, if an incredibly rare one. You have to specifically WANT an encoding error if you try to store something that, later on, will cause problems. It's like asking for a 32-bit signed integer type in Python, because you're using it for something where it's eventually going to be sent to something that can't use larger numbers. Not something that wants a core feature, usually. > The data doesn't actually represent text, but Python 2 style byte- > strings, and Latin-1 is just a convenient, easy way to get that that > ensures ASCII bytes look like ASCII characters. The OP is talking about JSON. Reason makes no sense in that context. And if it really is a byte string, why store it as a Latin-1 string? Store it as the type BLOB instead. Latin-1 is not "arbitrary bytes". It is a very specific encoding that cannot decode every possible byte value. Using Latin-1 to store arbitrary bytes is just as wrong as using ASCII to store eight-bit data. So, you've given me one possible reason that is EXTREMELY situational and, even there, could be handled differently. And it's only valid when you're working with something that supports more than ASCII and no more than Latin-1, and moreover, you have the need for non-ASCII characters. (Otherwise, just use ASCII, which you can declare as UTF-8 if you wish.) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
PyQt4 QWebView cant load google maps markers
I want to create a simple python app using pyqt,QWebView and google maps with markers. The problem is that,the markers does not load inside the QWebView, as you can see they load just fine in the browser. here the simple code : import sys from PyQt4.QtCore import * from PyQt4.QtGui import * from PyQt4.QtWebKit import * import os app = QApplication(sys.argv) web_view= dialog.findChild(QWebView,"webView") google='https://www.google.gr/maps/place/Granite+Mountain+Hotshots+Memorial+State+Park/@34.1749572,-112.850308,12.78z/data=!4m13!1m7!3m6!1s0x872b08ebcb4c186b:0x423927b17fc1cd71!2zzpHPgc65zrbPjM69zrEsIM6Xzr3Pic68zq3Ovc61z4IgzqDOv867zrnPhM61zq_Otc-C!3b1!8m2!3d34.0489281!4d-111.0937311!3m4!1s0x80d330577faee965:0x66d75aef24890ae1!8m2!3d34.2032843!4d-112.7746582?hl=el' web_view2.load(QUrl(google)) web_view2.show() sys.exit(app.exec_()) error message : right click and select Inspect, then go to console and observe the messages console I take this error : TypeError: 'undefined' is not a function (evaluating 'this.D.bind(this)') marker.js:58 Any idea how to can fix that ? I need to add more settings to QWebView to work google maps with markers ? -- https://mail.python.org/mailman/listinfo/python-list
Re: Putting Unicode characters in JSON
On Fri, 23 Mar 2018 12:05:34 +1100, Chris Angelico wrote:
> Latin-1 is not "arbitrary bytes". It is a very specific encoding that
> cannot decode every possible byte value.
Yes it can.
py> blob = bytes(range(256))
py> len(blob)
256
py> blob[45:55]
b'-./0123456'
py> s = blob.decode('latin1')
py> len(s)
256
py> s[45:55]
'-./0123456'
--
Steve
--
https://mail.python.org/mailman/listinfo/python-list
