python-2.x

StringIO and compatibility with 'with' statement (context manager)

醉酒当歌 提交于 2019-12-02 17:00:07
I have some legacy code with a legacy function that takes a filename as an argument and processes the file contents. A working facsimile of the code is below. What I want to do is not have to write to disk with some content that I generate in order to use this legacy function, so I though I could use StringIO to create an object in place of the physical filename. However, this does not work, as you can see below. I thought StringIO was the way to go with this. Can anyone tell me if there is a way to use this legacy function and pass it something in the argument that isn't a file on disk but

How can I optimize this Python code to generate all words with word-distance 1?

穿精又带淫゛_ 提交于 2019-12-02 16:14:21
Profiling shows this is the slowest segment of my code for a little word game I wrote: def distance(word1, word2): difference = 0 for i in range(len(word1)): if word1[i] != word2[i]: difference += 1 return difference def getchildren(word, wordlist): return [ w for w in wordlist if distance(word, w) == 1 ] Notes: distance() is called over 5 million times, majority of which is from getchildren, which is supposed to get all words in the wordlist that differ from word by exactly 1 letter. wordlist is pre-filtered to only have words containing the same number of letters as word so it's guaranteed

Python: UnicodeDecodeError: 'utf8' codec can't decode byte 0x91 [closed]

戏子无情 提交于 2019-12-02 15:57:49
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I'm parsing a CSV as follows: with open(args.csv, 'rU') as csvfile: try: reader = csv.DictReader(csvfile, dialect=csv.QUOTE_NONE) for row in reader: ... where args.csv is the name of my file. One of the rows in my file is an e with two dots on top. My script breaks when it encounters this. I get the following

How can I show status of current task running and update the progressbar without freezing at same time in python 2 tkinter?

纵然是瞬间 提交于 2019-12-02 15:49:46
问题 My code displays a button. When button is pressed, a filedialog appears to ask to user select a file (after a messagebox). No problem here. My problem occurs when I want update the progressbar and show the status of current task in execution. The GUI freezes, and the progressbar and task status are updated only after work was finished. Or, if anyone can give me a functional/similar example to do this, please. This is the actual file I'm working on (Python 2): # -*- coding: utf-8 -*- import os

Write a partial row with csv.DictWriter?

天涯浪子 提交于 2019-12-02 11:27:34
问题 I have a CSV file with a group of inputs. Example: A B C D I want to analyze the results and output a CSV file for each row such as: B C The DictReader builds the full dictionary with the keys 'A' 'B' 'C' 'D' in it as it should. The DictWriter sets up the fields as it is supposed to. However, when I attempt the send the data to the DictWriter , I get the error message: ValueError: dict contains fields not in fieldnames: A D Must I delete the value entries by hand from the dictionary or is

Can't get raw_input to return a number

醉酒当歌 提交于 2019-12-02 11:25:37
print "How old are you?", age = raw_input() print "How tall are you in inches?", height = raw_input() print "How much do you weigh in pounds", weight = raw_input() print "So, you are %r years old, %r inches tall, and %d kilograms." % ( age, height, weight / 2.2) So I am new to code and this is my code. When I use terminal to compile it, I get this: How old are you? 1 How tall are you in inches? 1 How much do you weigh in pounds 1 Traceback (most recent call last): File "ex11.py", line 9, in <module> age, height, weight / 2.2) TypeError: unsupported operand type(s) for /: 'str' and 'float' Can

Python: UnicodeDecodeError: 'utf8' codec can't decode byte 0x91 [closed]

柔情痞子 提交于 2019-12-02 11:13:12
I'm parsing a CSV as follows: with open(args.csv, 'rU') as csvfile: try: reader = csv.DictReader(csvfile, dialect=csv.QUOTE_NONE) for row in reader: ... where args.csv is the name of my file. One of the rows in my file is an e with two dots on top. My script breaks when it encounters this. I get the following stack trace: File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 244, in dumps return _default_encoder.encode(obj) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py"

How can I show status of current task running and update the progressbar without freezing at same time in python 2 tkinter?

强颜欢笑 提交于 2019-12-02 10:13:07
My code displays a button. When button is pressed, a filedialog appears to ask to user select a file (after a messagebox). No problem here. My problem occurs when I want update the progressbar and show the status of current task in execution. The GUI freezes, and the progressbar and task status are updated only after work was finished. Or, if anyone can give me a functional/similar example to do this, please. This is the actual file I'm working on (Python 2): # -*- coding: utf-8 -*- import os import Tkinter import ttk import tkMessageBox import tkFileDialog import base64 import threading

How do you implement multilingual support for pyqt4

时间秒杀一切 提交于 2019-12-02 08:48:01
问题 I have a pyqt4 program and would like to implement multilingual support. I have all the .qm files, but can't figure out how to use them. I can't really find much documentation on this, and nothing I try seems to work right. 回答1: There's tons of documentation on this subject, which can be found in the obvious places: Internationalization with Qt Qt Linguist Manual Internationalisation of PyQt4 Applications Below is a simple demo script (run with -h for usage): from PyQt4 import QtCore, QtGui

Python re match only letters from word

醉酒当歌 提交于 2019-12-02 08:36:46
I am new to Python re, but I need help. I searched here, google, documentation, but nothing worked. So here is what I am trying to do. I have word (for example) "string" then I have word list: strings, string, str, ing, in, ins, rs, stress And I want to matches like: string, str, ing, in, ins, rs. I don't want to match: stress, strings (because there are 2x s, and in word string, there is only 1) Simply match only the letters which are in word string . Sorry for bad english and if I didnt explained good enough. YES, and also, some letters are unicode. In the spirit of the question, here's a