Questions tagged «python»

Python是一种多范式,动态类型的多用途编程语言。它旨在快速学习,理解和使用并强制使用干净统一的语法。请注意,Python 2自2020年1月1日起已不再受支持。不过,对于特定于版本的Python问题,请添加[python-2.7]或[python-3.x]标签。使用Python变体或库(例如Jython,PyPy,Pandas,Numpy)时,请将其包含在标签中。

13
如何退出if子句
有哪些方法可以过早退出if子句? 有时候,我在编写代码时希望将一条break语句放在一个if子句中,只是要记住,这些语句只能用于循环。 让我们以以下代码为例: if some_condition: ... if condition_a: # do something # and then exit the outer if block ... if condition_b: # do something # and then exit the outer if block # more code here 我可以想到一种方法:假设退出情况发生在嵌套的if语句内,请将其余代码包装在一个大else块中。例: if some_condition: ... if condition_a: # do something # and then exit …

5
带有和不带括号的python断言
这是assert的四个简单调用: >>> assert 1==2 Traceback (most recent call last): File "<stdin>", line 1, in ? AssertionError >>> assert 1==2, "hi" Traceback (most recent call last): File "<stdin>", line 1, in ? AssertionError: hi >>> assert(1==2) Traceback (most recent call last): File "<stdin>", line 1, in ? AssertionError >>> assert(1==2, "hi") …

5
将列追加到熊猫数据框
这可能很容易,但是我有以下数据: 在数据框1中: index dat1 0 9 1 5 在数据框2中: index dat2 0 7 1 6 我想要一个具有以下形式的数据框: index dat1 dat2 0 9 7 1 5 6 我尝试使用该append方法,但是得到了交叉连接(即笛卡尔积)。 什么是正确的方法?
104 python  pandas 

7
sphinx-build失败-autodoc无法导入/查找模块
我正在尝试开始使用Sphinx,似乎遇到了很多问题。 命令: docs/sphinx-quickstart 我回答所有问题,一切正常。 命令: docs/ls 一切看起来都很正常。结果:build Makefile source 命令: sphinx-build -d build/doctrees source build/html 似乎有效。我能够打开index.html文件,并看到我想要的“外壳”。 当我尝试将实际的源代码作为source文件夹时,我遇到了问题。 命令: sphinx-build -d build/doctrees ../ys_utils build/html 结果: Making output directory... Running Sphinx v1.1.3 loading pickled environment... not yet created No builder selected, using default: html loading intersphinx inventory from http://docs.python.org/objects.inv... building [html]: targets …

7
从python中运行bash脚本
我的以下代码有问题: callBash.py: import subprocess print "start" subprocess.call("sleep.sh") print "end" sleep.sh: sleep 10 我希望10秒钟后打印“结束”。(我知道这是一个愚蠢的示例,我可以简单地在python中睡眠,但是这个简单的sleep.sh文件只是一个测试)
104 python  bash  call 

6
'dict'对象没有属性'has_key'
在Python中遍历图形时,我收到此错误: 'dict'对象没有属性'has_key' 这是我的代码: def find_path(graph, start, end, path=[]): path = path + [start] if start == end: return path if not graph.has_key(start): return None for node in graph[start]: if node not in path: newpath = find_path(graph, node, end, path) if newpath: return newpath return None 该代码旨在查找从一个节点到另一节点的路径。代码源:http : //cs.mwsu.edu/~terry/courses/4883/lectures/graphs.html 为什么会出现此错误,我该如何解决?


4
如何用python完全制作Android应用程序?[关闭]
关闭。此问题不符合堆栈溢出准则。它当前不接受答案。 想改善这个问题吗?更新问题,使其成为Stack Overflow 的主题。 2年前关闭。 改善这个问题 我想开发一个(相当简单)的Android应用程序,可通过Play商店进行分发。我想在python中完全这样做。但是,在线研究并没有启发我:大多数评论要么已经过时(> 1岁,并且我认为从那时起可能会更好地集成python),要么他们谈论在android中运行python(例如here)。 因此,我正在寻找有关以下问题的信息: 用python完全开发App是否可行-这样做的工具是什么?(例如,推荐Kivy吗?) 如果是这样:最佳软件环境是什么?(我尝试使用Android Studio失败,但是找不到在其中运行python代码的方法。) 我对应用程序开发非常陌生,非常感谢在python中而不是在Jave等中进行此操作的所有线索,我尚不知道。 非常感谢您的提前帮助。
104 android  python 

7
如何删除熊猫数据框的最后一行数据
我认为这应该很简单,但是我尝试了一些想法,但都没有成功: last_row = len(DF) DF = DF.drop(DF.index[last_row]) #<-- fail! 我尝试使用负索引,但这也会导致错误。我仍然会误解一些基本知识。
104 python  pandas 

3
sqlalchemy不为空选择
如何像在SQL中一样添加过滤器,以从特定列中选择非空值? SELECT * FROM table WHERE YourColumn IS NOT NULL; 如何使用SQLAlchemy过滤器执行相同操作? select = select(table).select_from(table).where(all_filters)
104 python  sqlalchemy 

5
连接到boto3 S3时如何指定凭据?
在boto上,当以这种方式连接到S3时,我通常指定我的凭据: import boto from boto.s3.connection import Key, S3Connection S3 = S3Connection( settings.AWS_SERVER_PUBLIC_KEY, settings.AWS_SERVER_SECRET_KEY ) 然后,我可以使用S3执行操作(在我的情况下,从存储桶中删除对象)。 使用boto3,我发现的所有示例都是这样的: import boto3 S3 = boto3.resource( 's3' ) S3.Object( bucket_name, key_name ).delete() 我无法指定我的凭据,因此所有尝试均因InvalidAccessKeyId错误而失败。 如何使用boto3指定凭据?


11
目标数据库不是最新的
我想迁移一个Flask应用程序。我正在使用Alembic。 但是,我收到以下错误。 Target database is not up to date. 在网上,我读到它与此有关。 http://alembic.zzzcomputing.com/zh-CN/latest/cookbook.html#building-an-up-to-date-database-from-scratch 不幸的是,我不太了解如何使数据库保持最新状态,以及在何处/如何编写链接中给出的代码。如果您有迁移的经验,能否请您为我解释一下 谢谢

4
如何在Python中正确获取异常消息
从Python标准库的组件中获取异常消息的最佳方法是什么? 我注意到在某些情况下,您可以通过如下message字段获取它: try: pass except Exception as ex: print(ex.message) 但在某些情况下(例如在套接字错误的情况下),您必须执行以下操作: try: pass except socket.error as ex: print(ex) 我想知道是否有标准方法可以涵盖大多数情况?


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.