python-2.x

Python doctests and unicode

雨燕双飞 提交于 2019-12-01 06:25:42
I have a code base that runs unchanged in Python 2.7 and 3.2+. But the doctests in the documentation rst files are giving me a headache. When I run them in Python2, I get UnicodeEncodeError: 'ascii' codec can't encode character u'\xb2' in position 16: ordinal not in range(128) . If I add .. testsetup:: * from __future__ import unicode_literals then I get a lot of errors like Expected: 'something' Got: u'something' Is there a way to have doctest containing unicode characters in the rst files that work unchanged in Python 2.7 and 3.2+? Make sure you are using Python 3.3. It added the explicit u

Python: Convert tuple to comma separated String

ぃ、小莉子 提交于 2019-12-01 06:25:18
import MySQLdb db = MySQLdb.connect("localhost","root","password","database") cursor = db.cursor() cursor.execute("SELECT id FROM some_table") u_data = cursor.fetchall() >>> print u_data ((1320088L,),) What I found on internet got me till here: string = ((1320088L,),) string = ','.join(map(str, string)) >>> print string (1320088L,) what I expect output to look like: #Single element expected result 1320088L #comma separated list if more than 2 elements, below is an example 1320088L,1320089L Use itertools.chain_fromiterable() to flatten your nested tuples first, then map() to string and join() .

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

醉酒当歌 提交于 2019-12-01 04:40:39
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 a difference between '5' and u'5' in this regard? It works like this 1 . >>> float() == long() == int()

Python doctests and unicode

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 04:12:50
问题 I have a code base that runs unchanged in Python 2.7 and 3.2+. But the doctests in the documentation rst files are giving me a headache. When I run them in Python2, I get UnicodeEncodeError: 'ascii' codec can't encode character u'\xb2' in position 16: ordinal not in range(128) . If I add .. testsetup:: * from __future__ import unicode_literals then I get a lot of errors like Expected: 'something' Got: u'something' Is there a way to have doctest containing unicode characters in the rst files

Python 2.7 with pyuno

三世轮回 提交于 2019-12-01 04:05:07
问题 I am having python 2.7 installed on my windows 7. I have installed Libre Office 3.4 which comes with python 2.6. Hence i am not able to import uno and unohelper even after setting all environment variables. I need python 2.7 thus cannot revert back to python 2.6, as i earlier had python 2.6 working with open office 3. I recently changed to LibreOffice 3.4 and Python 2.7(ActivePython 2.7). Is there any way i can make python 2.7 to import uno and unohelper of LibreOffice 3.4 Thanks in advance.

TypeError: expected a character buffer object

只愿长相守 提交于 2019-12-01 04:03:43
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 thefile f = open("output"+'.csv','w') f.seek(0) f.write(thefile) f.close() and here is the complete

Unsupported characters in input

允我心安 提交于 2019-12-01 03:58:53
问题 I want to assign a string of characters to a variable but it says : there isn't a "code to show. I have a string that i want to assign to a variable d="stunning:/ËstÊnɪÅ/" Unsupported characters in input or word="stuning:/ˈstraɪkɪŋ/" Unsupported characters in input so basically the interpreter doesn't allow me to assign it to a variable, so I can't code on it. How can I extract, delete those characters from the text, or is there anything to do , so python will support this kind of input. I

Why do new style class and old style class have different behavior in this case?

非 Y 不嫁゛ 提交于 2019-12-01 03:13:22
I found something interesting, here is a snippet of code: class A(object): def __init__(self): print "A init" def __del__(self): print "A del" class B(object): a = A() If I run this code, I will get: A init But if I change class B(object) to class B() , I will get: A init A del I found a note in the __del__ doc : It is not guaranteed that del () methods are called for objects that still exist when the interpreter exits. Then, I guess it's because that B.a is still referenced(referenced by class B ) when the interpreter exists. So, I added a del B before the interpreter exists manually, and

python closure with assigning outer variable inside inner function

為{幸葍}努か 提交于 2019-12-01 03:05:15
I've got this piece of code: #!/usr/bin/env python def get_match(): cache=[] def match(v): if cache: return cache cache=[v] return cache return match m = get_match() m(1) if I run it, it says: UnboundLocalError: local variable 'cache' referenced before assignment but if I do this: #!/usr/bin/env python def get(): y = 1 def m(v): return y + v return m a=get() a(1) it runs. Is there something with list? or my code organizing is wrong? The problem is that the variable cache is not in the scope of the function match. This is not a problem if you only want to read it as in your second example, but

Why does Python 2's raw_input output unicode strings?

牧云@^-^@ 提交于 2019-12-01 02:58:18
问题 I tried the following on Codecademy's Python lesson hobbies = [] # Add your code below! for i in range(3): Hobby = str(raw_input("Enter a hobby:")) hobbies.append(Hobby) print hobbies With this, it works fine but if instead I try Hobby = raw_input("Enter a hobby:") I get [u'Hobby1', u'Hobby2', u'Hobby3'] . Where are the extra u s coming from? 回答1: The question's subject line might be a bit misleading: Python 2's raw_input() normally returns a byte string, NOT a Unicode string. However, it