python-3.3

Seeking from end of file throwing unsupported exception

徘徊边缘 提交于 2019-11-26 11:26:15
问题 I have this code snippet and I\'m trying to seek backwards from the end of file using python: f=open(\'D:\\SGStat.txt\',\'a\'); f.seek(0,2) f.seek(-3,2) This throws the following exception while running: f.seek(-3,2) io.UnsupportedOperation: can\'t do nonzero end-relative seeks Am i missing something here? 回答1: From the documentation for Python 3.2 and up: In text files (those opened without a b in the mode string), only seeks relative to the beginning of the file are allowed (the exception

Python 3: ImportError “No Module named Setuptools”

烈酒焚心 提交于 2019-11-26 10:16:25
I'm having troubles with installing packages in Python 3. I have always installed packages with setup.py install . But now, when I try to install the ansicolors package I get: importerror "No Module named Setuptools" I have no idea what to do because I didn't have setuptools installed in the past. Still, I was able to install many packages with setup.py install without setuptools. Why should I get setuptools now? I can't even install setuptools because I have Python 3.3 and setuptools doesn't support Python 3. Why doesn't my install command work anymore? tiago Your setup.py file needs

Import arbitrary python source file. (Python 3.3+)

倾然丶 夕夏残阳落幕 提交于 2019-11-26 08:15:26
问题 How can I import an arbitrary python source file (whose filename could contain any characters, and does not always ends with .py ) in Python 3.3+ ? I used imp.load_module as follows: >>> import imp >>> path = \'/tmp/a-b.txt\' >>> with open(path, \'U\') as f: ... mod = imp.load_module(\'a_b\', f, path, (\'.py\', \'U\', imp.PY_SOURCE)) ... >>> mod <module \'a_b\' from \'/tmp/a-b.txt\'> It still works in Python 3.3, but according to imp.load_module documentation, it is deprecated: Deprecated

SyntaxError: multiple statements found while compiling a single statement

最后都变了- 提交于 2019-11-26 06:48:59
问题 I\'m in Python 3.3 and I\'m only entering these 3 lines: import sklearn as sk import numpy as np import matplotlib.pyplot as plt I\'m getting this error: SyntaxError: multiple statements found while compiling a single statement What could I be doing wrong? Edit: If anyone comes across this question, the solution I found was to download Idlex and use its IDLE version, which allows multiple lines. Screenshot: http://imgur.com/AJSrhhD 回答1: In the shell, you can't execute more than one statement

Writing to CSV with Python adds blank lines [duplicate]

时间秒杀一切 提交于 2019-11-26 06:27:08
问题 This question already has an answer here: CSV file written with Python has blank lines between each row 8 answers I am trying to write to CSV file but there are blank rows in between. How can I remove the blank rows? import csv b = open(\'test.csv\', \'w\') a = csv.writer(b) data = [[\'Me\', \'You\'],\\ [\'293\', \'219\'],\\ [\'54\', \'13\']] a.writerows(data) b.close() 回答1: The way you use the csv module changed in Python 3 in several respects (docs), at least with respect to how you need to

How to install pip for Python 3 on Mac OS X?

。_饼干妹妹 提交于 2019-11-26 05:14:09
问题 OS X (Mavericks) has Python 2.7 stock installed. But I do all my own personal Python stuff with 3.3. I just flushed my 3.3.2 install and installed the new 3.3.3. So I need to install pyserial again. I can do it the way I\'ve done it before, which is: Download pyserial from pypi untar pyserial.tgz cd pyserial python3 setup.py install But I\'d like to do like the cool kids do, and just do something like pip3 install pyserial . But it\'s not clear how I get to that point. And just that point.

How to make program go back to the top of the code instead of closing [duplicate]

拜拜、爱过 提交于 2019-11-26 04:51:53
问题 This question already has answers here : Asking the user for input until they give a valid response (18 answers) Closed 3 years ago . I\'m trying to figure out how to make Python go back to the top of the code. In SmallBasic, you do start: textwindow.writeline(\"Poo\") goto start But I can\'t figure out how you do that in Python :/ Any ideas anyone? The code I\'m trying to loop is this #Alan\'s Toolkit for conversions def start() : print (\"Welcome to the converter toolkit made by Alan.\") op

hash function in Python 3.3 returns different results between sessions

夙愿已清 提交于 2019-11-26 03:41:17
问题 I\'ve implemented a BloomFilter in python 3.3, and got different results every session. Drilling down this weird behavior got me to the internal hash() function - it returns different hash values for the same string every session. Example: >>> hash(\"235\") -310569535015251310 ----- opening a new python console ----- >>> hash(\"235\") -1900164331622581997 Why is this happening? Why is this useful? 回答1: Python uses a random hash seed to prevent attackers from tar-pitting your application by

Python 3: ImportError “No Module named Setuptools”

自古美人都是妖i 提交于 2019-11-26 02:06:52
问题 I\'m having troubles with installing packages in Python 3. I have always installed packages with setup.py install . But now, when I try to install the ansicolors package I get: importerror \"No Module named Setuptools\" I have no idea what to do because I didn\'t have setuptools installed in the past. Still, I was able to install many packages with setup.py install without setuptools. Why should I get setuptools now? I can\'t even install setuptools because I have Python 3.3 and setuptools

Why is dictionary ordering non-deterministic?

天涯浪子 提交于 2019-11-25 23:50:06
问题 I recently switched from Python 2.7 to Python 3.3, and it seems that while in Python 2 the ordering of dictionary keys was arbitrary but consistent, in Python 3 the ordering of the keys of a dictionary obtained with e.g. vars() appears non-deterministic. If I run: class Test(object): pass parameters = vars(Test) print(list(parameters.keys())) in both Python 2.7 and Python 3.3, then: Python 2.7 consistently gives me [\'__dict__\', \'__module__\', \'__weakref__\', \'__doc__\'] With Python 3.3,