python-2.x

Tkinter not working mac osx el capitan

守給你的承諾、 提交于 2019-12-11 14:14:26
问题 I was trying to import using Python2.7 the module Tkinter in MAC OSX el capitan Version 10.11, but I have the following error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module> import _tkinter # If this fails your Python may not be configured for Tk ImportError: No module named _tkinter I would like to get a solution for this problem, because seems to

Python 2 __missing__ method

时间秒杀一切 提交于 2019-12-11 13:14:52
问题 I wrote a very simple program to subclass a dictionary. I wanted to try the __missing__ method in python. After some research i found out that in Python 2 it's available in defaultdict . ( In python 3 we use collections.UserDict though..) The __getitem__ is the on responsible for calling the __missing__ method if the key isn't found. When i implement __getitem__ in the following program i get a key error, but when i implement without it, i get the desired value. import collections class

2to3 ParseError in python file

血红的双手。 提交于 2019-12-11 12:28:03
问题 I tried running 2to3 on a python file however it is failing with following error, i am not able to understand where exactly in the problem any help is appreciated. [adsf@localhost direct]$ 2to3 ./views/statusformatter.py RefactoringTool: Skipping implicit fixer: buffer RefactoringTool: Skipping implicit fixer: idioms RefactoringTool: Skipping implicit fixer: set_literal RefactoringTool: Skipping implicit fixer: ws_comma RefactoringTool: Can't parse ./views/statusformatter.py: ParseError: bad

Adding another column to a CSV file w/ python

喜夏-厌秋 提交于 2019-12-11 12:20:20
问题 I am looking to add another column to a CSV file w/ python. file1 is Date.csv has format ID, Date 0,"Jan 22, 2016" 1,"Jan 21, 2016" 2,"Jan 20, 2016" 3,"Jan 19, 2016" and file2 is Price.csv ID, Price 0,27.89 1,26.80 2,26.78 3,26.00 My desired output is (in Date.csv) ID, Date 0,"Jan 22, 2016", 27.89 1, "Jan 21, 2016", 26.80 2, "Jan 20, 2016", 26.78 3, "Jan 19, 2016", 26.00 but what I'm returning is the price repeating 0,27.89,27.89 1,26.80,26.80 2,26.78,26.78 3,26.00,26.00 My program is as

how to call python scripts from php

≡放荡痞女 提交于 2019-12-11 12:04:57
问题 I need to pass a value from PHP to a Python script which then writes the value into a csv file. But, I'm having difficulties, my python when called writes an empty csv file. What could be the problem. <?php if (isset($_POST['data'])){ $data = $_POST['data']; $result = exec("python processData.py .$data"); echo $result; } ?> and processData.py import nltk from nltk.corpus import stopwords from nltk import stem import re import sys import csv mysentence = sys.argv[1] f = open("output.csv", "wb"

OpenCV: Auomatic resize of the window to the image displayed

女生的网名这么多〃 提交于 2019-12-11 11:23:37
问题 Is there a way to force the window displayed by OpenCV ( cv2.imshow() )when displaying an image to fit to the width and height of the image without the need to resize by the mouse it for that ? 回答1: You need to pass CV_WINDOW_AUTOSIZE when creating the namedWindow (or WINDOW_AUTOSIZE if you import cv2 instead of cv ) Here is an example: cv2.namedWindow("window", cv2.WINDOW_AUTOSIZE) # or cv.namedWindow("window",cv.CV_WINDOW_AUTOSIZE) cv2.imshow("window", yourimage) 来源: https://stackoverflow

How to append a new list to an existing CSV file?

*爱你&永不变心* 提交于 2019-12-11 06:48:34
问题 I already have a CSV file created from a list using CSV writer. I want to append another list created through a for loop columnwise to a CSV file. The first code to create a CSV file is as follows: with open("output.csv", "wb") as f: writer = csv.writer(f) for row in zip(master_lst): writer.writerow(row) I created the CSV file using the list master_lst and the output is as follows: read ACACCUGGGCUCUCCGGGUACC ACGGCUACCUUCACUGCCACCC AGGCAGUGUGGUUAGCUGGUUG Then I create another list ( ind_lst )

Why is __slots__ behaving differently in Python 2 and 3 when inheriting from an abstract base class

荒凉一梦 提交于 2019-12-11 03:53:06
问题 I created the following class to store changeable points on a plane in a memory-efficient manner - I need a mutable equivalent of namedtuple('Point', 'x y') . Since instance dictionaries are big, I thought I'd go for __slots__ : from collections import Sequence class Point(Sequence): __slots__ = ('x', 'y') def __init__(self, x=0, y=0): self.x = x self.y = y def __getitem__(self, item): return getattr(self, self.__slots__[item]) def __setitem__(self, item, value): return setattr(self, self._

Capitalize a substring within a string

。_饼干妹妹 提交于 2019-12-11 03:27:16
问题 I'm trying to create something like: string: How do you do today? substring: o >>> hOw dO yOu dO tOday? I've already written the rest of the code (prompting for strings etc.), I am just stuck on having to capitalize the substring within the string. 回答1: >>> s='How do you do today?' >>> sub_s='o' >>> s.replace(sub_s, sub_s.upper()) 'HOw dO yOu dO tOday?' And can get more complicated if you only want to change some (i.e., the 2nd one), one liner: >>> ''.join([item.upper() if i==[idx for idx, w

What should I use instead of .__getslice__?

余生长醉 提交于 2019-12-11 03:15:53
问题 I'm in the process of porting a library to Python 3. I found this method: def __getslice__(self, index, listget=list.__getslice__): self._resolve() return listget(self, index) which raises an error since .__getslice__ is deprecated. I looked at the documentation and it seems like .__getitem__ is what most people are using to replace .__getslice__ . The only issue is that this library has a method exactly like the above method except it's called __getitem__ and listget=list.__getitem__ ). I