python-2.x

Converting “yield from” statement to Python 2.7 code

白昼怎懂夜的黑 提交于 2019-12-17 02:47:27
问题 I had a code below in Python 3.2 and I wanted to run it in Python 2.7. I did convert it (have put the code of missing_elements in both versions) but I am not sure if that is the most efficient way to do it. Basically what happens if there are two yield from calls like below in upper half and lower half in missing_element function? Are the entries from the two halves (upper and lower) appended to each other in one list so that the parent recursion function with the yield from call and use both

Why does Python print unicode characters when the default encoding is ASCII?

妖精的绣舞 提交于 2019-12-16 22:21:31
问题 From the Python 2.6 shell: >>> import sys >>> print sys.getdefaultencoding() ascii >>> print u'\xe9' é >>> I expected to have either some gibberish or an Error after the print statement, since the "é" character isn't part of ASCII and I haven't specified an encoding. I guess I don't understand what ASCII being the default encoding means. EDIT I moved the edit to the Answers section and accepted it as suggested. 回答1: Thanks to bits and pieces from various replies, I think we can stitch up an

Writing Unicode text to a text file?

◇◆丶佛笑我妖孽 提交于 2019-12-16 20:12:51
问题 I'm pulling data out of a Google doc, processing it, and writing it to a file (that eventually I will paste into a Wordpress page). It has some non-ASCII symbols. How can I convert these safely to symbols that can be used in HTML source? Currently I'm converting everything to Unicode on the way in, joining it all together in a Python string, then doing: import codecs f = codecs.open('out.txt', mode="w", encoding="iso-8859-1") f.write(all_html.encode("iso-8859-1", "replace")) There is an

How to extract numbers from a list of strings?

最后都变了- 提交于 2019-12-14 03:58:13
问题 How should I extract numbers only from a = ['1 2 3', '4 5 6', 'invalid'] I have tried: mynewlist = [s for s in a if s.isdigit()] print mynewlist and for strn in a: values = map(float, strn.split()) print values Both failed because there is a space between the numbers. Note: I am trying to achieve output as: [1, 2, 3, 4, 5, 6] 回答1: I think you need to process each item in the list as a split string on whitespace. a = ['1 2 3', '4 5 6', 'invalid'] numbers = [] for item in a: for subitem in item

Unable to show an image using python PIL Image.show

蹲街弑〆低调 提交于 2019-12-14 02:08:01
问题 I'm using the Python Imaging Library and I am unable to open an image successfully in Windows Live Photo Gallery. There is a message that shows up saying "There are no photos or videos selected" instead of the image. This is what I've tried: import Image img = Image.open(r"C:\Users\User\Pictures\image.jpg") img.show() This is pretty much the same as in the PIL handbook tutorial, so I'm not sure where I'm going wrong. 回答1: The documentation says: On Windows, it [show()] saves the image to a

pyforms cannot import conf from pysettings

狂风中的少年 提交于 2019-12-13 17:36:38
问题 This is my python file: import pyforms import pyside from pyforms import BaseWidget from pyforms.Controls import ControlText from pyforms.Controls import ControlButton class SimpleExample1(BaseWidget): def __init__(self): super(SimpleExample1,self).__init__('Simple example 1') #Definition of the forms fields self._firstname = ControlText('First name', 'Default value') self._middlename = ControlText('Middle name') self._lastname = ControlText('Lastname name') self._fullname = ControlText('Full

Number changing when inserted into list [duplicate]

☆樱花仙子☆ 提交于 2019-12-13 17:11:37
问题 This question already has answers here : python floating number [duplicate] (4 answers) Closed 5 years ago . I'm only going to paste part of my code since it's very long, but I was wondering if any one might know potential causes of this problem. So I have this code here: print "part a", working_weight cells[working_cell_position][4] = working_weight print "part b", working_weight, cells[working_cell_position][4], cells[working_cell_position] and what it prints is this: part a 62.4 part b 62

Multiple installs of Python on MacOSX for Eclipse

十年热恋 提交于 2019-12-13 15:22:43
问题 I want to have multiple installs of Python: 2.1, 2.4, 2.7, 3.x My IDE is Eclipse (Helios)/Pydev on MacOSX, which works great. I have a couple of Python codebases that are/will be running on different versions of Python. Also, I like Eclipse PyDev's crosslinking from source-code to documentation. The standard recommendation seems to be: use virtualenv, and keep the installs totally separate from the builtin MacPython (2.6.1). Eclipse should never be pointing to the MacPython install. (Should

Python: TypeError: list indices must be integers, not str

这一生的挚爱 提交于 2019-12-13 12:56:33
问题 I'm going to do Matrix Addition on Python.(Not finish). But it shows an error. m, n = (int(i) for i in raw_input().split()) a = [[0 for i in range(m)] for j in range(n)] b = [[0 for i in range(m)] for j in range(n)] c = [] total = [] for i in range(m): x = raw_input() for j in range(n): value = [int(i) for i in x.split()] c[i][j] = a[i][j] #c.append(value) print a for i in c: print i I want to input 3 3 <-- matrix dimensional m*n 1 2 3 > 3 2 1 > matrix A 1 3 2 > 1 1 1 > 1 1 1 > matrix B 1 1 1

How to install immlib module in python?

让人想犯罪 __ 提交于 2019-12-13 06:43:50
问题 I have Python 2.7 installed on my windows machine. I've downloaded and installed Immunity Debugger. But when I try to import immlib or immutils module in python, it says no such module. How to install these modules? Using pip, it says no such repository. Please help. 回答1: It goes with Immunity Debugger. Install immunity debugger and your immlib.py will be there. 回答2: Type "system settings" in the cortana/start bar search box Click "View Advanced System Settings In the Advanced tab in the