python-2.x

A number smaller than negative infinity in python? [duplicate]

扶醉桌前 提交于 2019-12-20 02:05:36
问题 This question already has answers here : Is everything greater than None? (2 answers) Closed last month . This is possible in python2: None < float('-inf') Also, it always returns True However, on python3, this throws TypeError: unorderable types: NoneType() < int() Why is None comparable to integers/floats with python2? Are there any benefits or applications to None being orderable in python2? 回答1: First of all Python 2 allowed comparing all types of mixed types. This wart was fixed in

Python how to decode unicode with hex characters

偶尔善良 提交于 2019-12-19 19:45:00
问题 I have extracted a string from web crawl script as following: u'\xe3\x80\x90\xe4\xb8\xad\xe5\xad\x97\xe3\x80\x91' I want to decode u'\xe3\x80\x90\xe4\xb8\xad\xe5\xad\x97\xe3\x80\x91' with utf-8. With http://ddecode.com/hexdecoder/, I can see the result is '【中字】' I tried using the following syntax but failed. msg = u'\xe3\x80\x90\xe4\xb8\xad\xe5\xad\x97\xe3\x80\x91' result = msg.decode('utf8') Error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib

Python how to decode unicode with hex characters

笑着哭i 提交于 2019-12-19 19:44:59
问题 I have extracted a string from web crawl script as following: u'\xe3\x80\x90\xe4\xb8\xad\xe5\xad\x97\xe3\x80\x91' I want to decode u'\xe3\x80\x90\xe4\xb8\xad\xe5\xad\x97\xe3\x80\x91' with utf-8. With http://ddecode.com/hexdecoder/, I can see the result is '【中字】' I tried using the following syntax but failed. msg = u'\xe3\x80\x90\xe4\xb8\xad\xe5\xad\x97\xe3\x80\x91' result = msg.decode('utf8') Error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib

Why is int(50)<str(5) in python 2.x?

☆樱花仙子☆ 提交于 2019-12-19 07:22:06
问题 In python 3, int(50)<'2' causes a TypeError , and well it should. In python 2.x, however, int(50)<'2' returns True (this is also the case for other number formats, but int exists in both py2 and py3). My question, then, has several parts: Why does Python 2.x (< 3?) allow this behavior? (And who thought it was a good idea to allow this to begin with???) What does it mean that an int is less than a str ? Is it referring to ord / chr ? Is there some binary format which is less obvious? Is there

Why is int(50)<str(5) in python 2.x?

自古美人都是妖i 提交于 2019-12-19 07:21:43
问题 In python 3, int(50)<'2' causes a TypeError , and well it should. In python 2.x, however, int(50)<'2' returns True (this is also the case for other number formats, but int exists in both py2 and py3). My question, then, has several parts: Why does Python 2.x (< 3?) allow this behavior? (And who thought it was a good idea to allow this to begin with???) What does it mean that an int is less than a str ? Is it referring to ord / chr ? Is there some binary format which is less obvious? Is there

TypeError: expected a character buffer object

拈花ヽ惹草 提交于 2019-12-19 05:56:15
问题 I am trying to write a list of a list to a new file, but I am getting this error: Traceback (most recent call last): File "", line 1, in dowork() File "C:\Python27\work\accounting\formatting quickbooks file\sdf.py", line 11, in dowork WriteFile() File "C:\Python27\work\accounting\formatting quickbooks file\sdf.py", line 71, in WriteFile f.write(thefile) TypeError: expected a character buffer object How do I write a list of a list to a file? This is how i am writing: def WriteFile(): global

bytes vs bytearray in Python 2.6 and 3

天大地大妈咪最大 提交于 2019-12-19 05:12:55
问题 I'm experimenting with bytes vs bytearray in Python 2.6. I don't understand the reason for some differences. A bytes iterator returns strings: for i in bytes(b"hi"): print(type(i)) Gives: <type 'str'> <type 'str'> But a bytearray iterator returns int s: for i in bytearray(b"hi"): print(type(i)) Gives: <type 'int'> <type 'int'> Why the difference? I'd like to write code that will translate well into Python 3. So, is the situation the same in Python 3? 回答1: In Python 2.6 bytes is merely an

Relationship between pickle and deepcopy

假装没事ソ 提交于 2019-12-19 05:06:07
问题 What exactly is the relationship between pickle and copy.deepcopy ? What mechanisms do they share, and how? It is clear the two are closely-related operations, and share some of the mechanisms/protocols, but I can't wrap my head around the details. Some (confusing) things I found out: If a class defines __[gs]etstate__ , they get called upon a deepcopy of its instances. This surprised me at first, because I thought they are specific to pickle , but then I found that Classes can use the same

Determine consecutive dates

ε祈祈猫儿з 提交于 2019-12-19 04:38:31
问题 I have a list of datetime.dates and I need to check if each date is from the next consecutive month. Hope it's clear what do I mean from the code: import datetime from unittest import TestCase def is_consecutive(dates): # TODO return class DatesTestCase(TestCase): def test_consecutive(self): self.assertTrue(is_consecutive([datetime.date(2010, 10, 3), datetime.date(2010, 11, 8), datetime.date(2010, 12, 1), datetime.date(2011, 01, 11)])) def test_not_consecutive(self): self.assertFalse(is

Compare result from hexdigest() to a string

五迷三道 提交于 2019-12-19 02:54:13
问题 I've got a generated MD5-hash, which I would like to compare to another MD5-hash from a string. The statement below is false, even though they look the same when you print them and should be true. hashlib.md5("foo").hexdigest() == "acbd18db4cc2f85cedef654fccc4a4d8" Google told me that I should encode the result from hexdigest() , since it doesn't return a string. However, the code below doesn't seem to work either. hashlib.md5("foo").hexdigest().encode("utf-8") == "foo".encode("utf-8") 回答1: