草庐IT

try-convert

全部标签

python - 对自定义 Exception 的 try/except 感到困惑

我的代码:classAError(Exception):print'erroroccur'foriinrange(3):try:print'---oo'raiseAErrorexceptAError:print'getAError'else:print'goingon'finally:print'finally'当我运行上面的代码时,输​​出是这样的:erroroccur---oogetAErrorfinally---oogetAErrorfinally---oogetAErrorfinally我认为字符串"erroroccur"应该出现三次,比如"---oo",但它只出现了一次;为什

python - PyQt : Trying to understand graphics scene/view

我正在尝试理解QGraphicsView和QGraphicsScene,特别是如何放置图形项目并让它们出现在我想要的位置。如果场景大于查看区域,我也很困惑何时会出现滚动条。例如,这段代码将创建一个在顶角带有椭圆的小图形View:importsysfromPyQt4importQtGui,QtCoreclassMyView(QtGui.QGraphicsView):def__init__(self):QtGui.QGraphicsView.__init__(self)self.scene=QtGui.QGraphicsScene(self)self.scene.setSceneRect(

python - 统一码编码错误 : 'ascii' codec can't encode character when trying a HTTP POST in Python

我正在尝试使用Unicode字符串(u'\xe4\xf6\xfc')作为Python中的参数执行HTTPPOST,但我收到以下错误:UnicodeEncodeError:'ascii'编解码器无法编码字符这是用于制作HTTPPOST的代码(使用httplib2)http=httplib2.Http()userInfo=[('Name',u'\xe4\xf6\xfc')]data=urlencode(userInfo)resp,content=http.request(url,'POST',body=data)关于如何解决这个问题有什么想法吗? 最佳答案

Python pytz : convert local time to utc. Localize 似乎没有转换

我有一个将日期时间存储为UTC的数据库。我需要查找特定时间的信息,但日期和时间是以本地时间给出的,比方说“欧洲/哥本哈根”。我得到的是:year=2012;month=12;day=2;hour=13;min=1;因此,我需要将它们转换为UTC,以便我可以在数据库中查找它们。我想使用pytz来做到这一点。我正在查看localize:local_tz=timezone('Europe/Copenhagen')t=local_tz.localize(datetime.datetime(year,month,day,hour,min))但我对localize()感到困惑。这是假设年份等是在本

python - psycopg2 类型错误 : not all arguments converted during string formatting

我正在尝试执行一个简单的查询,但无论我如何传递参数都会出现此错误。这是查询(我正在使用Trac数据库对象连接到数据库):cursor.execute("""SELECTnameFROM"%s".customerWHEREfirm_id='%s'"""%(schema,each['id']))schema和each['id']都是简单的字符串print("""SELECTnameFROM"%s".customerWHEREfirm_id='%s'"""%(schema,each['id']))结果:SELECTnameFROM"Planing".customerWHEREfirm_id=

python - 加密 : AssertionError ("PID check failed. RNG must be re-initialized after fork(). Hint: Try Random.atfork()")

我正在创建执行不同任务的各种流程。其中之一,也是唯一一个,有一个创建PyCrypto对象的安全模块。所以我的程序启动,创建各种进程,处理消息的进程使用安全模块解密,我得到以下错误:firstSymKeybin=self.cipher.decrypt(encFirstSymKeybin,'')File"/usr/local/lib/python2.7/dist-packages/Crypto/Cipher/PKCS1_v1_5.py",line206,indecryptm=self._key.decrypt(ct)File"/usr/local/lib/python2.7/dist-pa

python - numpy 与 python : convert 3d array to 2d

假设我有一张彩色图像,自然会用Python中的3维数组表示,形状为(nxmx3)并将其称为img。我想要一个新的二维数组,将其称为“narray”,形状为(3,nxm),这样该数组的每一行分别包含R、G和Bchannel的“展平”版本。此外,它应该具有我可以通过类似的方式轻松重建任何原始channel的属性narray[0,].reshape(img.shape[0:2])#sothisshouldreconstructbacktheRchannel.问题是如何从“img”构建“narray”?简单的img.reshape(3,-1)不起作用,因为我不希望元素的顺序。谢谢

python - 错误 : No Commands supplied when trying to install pyglet

我已经下载了pyglet,但是当我运行“setup.py”,它只是在命令行:Traceback(mostrecentcalllast):File"C:\PythonX\Include\pyglet\pyglet-1.1.4\setup.py",line285,insetup(**setup_info)File"C:\Python27\lib\distutils\core.py",line140,insetupraiseSystemExit,gen_usage(dist.script_name)+"\nerror:%s"%msgSystemExit:usage:setup.py[glob

python - Python中如何正确使用try,except,else

所以我想知道编写tryexcept语句的正确方法是什么。我是Python错误处理的新手。选项1try:itemCode=items["itemCode"]dbObject=db.GqlQuery("SELECT*FROM%sWHEREcode=:1"%dbName,itemCode).get()dbObject.delete()exceptAttributeError:print"There'snoitemwiththatcode"exceptKeyError:print"Badparametername"except:print"Unknowerror"选项2try:itemCode

python - 使用 ndb 的 GAE 错误 - BadQueryError : Cannot convert FalseNode to predicate

我有一个使用python在GoogleAppEngine上运行的应用程序。模型类扩展自ndb(google.appengine.ext.ndb)类。我的一个View对数据库进行异步调用,或多或少类似于:#ExerciseListLogisandbmodelclass#start_current,end_currentaredates#student_idisastring#contentsisalistofkeysexercise_log_query=ExerciseListLog.query(ndb.AND(ExerciseListLog.creation>=start_curren