Questions tagged «python»

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

9
具有可变深度的多级defaultdict?
我有很多类似的清单: [A][B1][C1]=1 [A][B1][C2]=2 [A][B2]=3 [D][E][F][G]=4 我想建立一个多层次的字典,像: A --B1 -----C1=1 -----C2=1 --B2=3 D --E ----F ------G=4 我知道,如果我用递归defaultdict我可以写table[A][B1][C1]=1,table[A][B2]=2但如果我硬编码的INSERT语句这只适用。 解析列表时,我不需要事先调用多少[]个table[key1][key2][...]。

7
如何缩进Python列表理解?
列表理解在某些情况下可能很有用,但阅读起来也很恐怖。作为一个稍微夸张的示例,您如何缩进以下内容? allUuids = [x.id for x in self.db.query(schema.allPostsUuid).execute(timeout = 20) if x.type == "post" and x.deleted is not False]



4
Python-什么是内置装饰器?[关闭]
关闭。此问题不符合堆栈溢出准则。它当前不接受答案。 想改善这个问题吗?更新问题,使其成为Stack Overflow的主题。 5年前关闭。 改善这个问题 我知道@staticmethod,@classmethod和@property,而只能通过零星的文档。Python内置的所有函数装饰器是什么?是在文档中吗?是否在某处维护了最新列表?
70 python  decorator 

3
在python中捕获特定的HTTP错误
我想捕获一个特定的http错误,而不是整个家庭中的任何一个..我想做的是- import urllib2 try: urllib2.urlopen("some url") except urllib2.HTTPError: <whatever> 但是我最终捕获的是任何一种HTTP错误,但是我只想在指定的网页不存在的情况下捕获!可能是HTTP错误404 ..但我不知道如何指定仅捕获错误404并让系统为其他事件运行默认处理程序。
70 python  http  urllib2  urllib 

2
python argh / argparse:如何将列表作为命令行参数传递?
我正在尝试使用argh库将参数列表传递给python脚本。可以接受类似以下内容的东西: ./my_script.py my-func --argA blah --argB 1 2 3 4 ./my_script.py my-func --argA blah --argB 1 ./my_script.py my-func --argA blah --argB 我的内部代码如下所示: import argh @argh.arg('--argA', default="bleh", help='My first arg') @argh.arg('--argB', default=[], help='A list-type arg--except it\'s not!') def my_func(args): "A function that does something" print args.argA print args.argB for b in …

2
如何使用python numpy.savetxt将字符串和浮点数写入ASCII文件?
我有一组包含字符串和浮点数的列表,例如: import numpy as num NAMES = num.array(['NAME_1', 'NAME_2', 'NAME_3']) FLOATS = num.array([ 0.5 , 0.2 , 0.3 ]) DAT = num.column_stack((NAMES, FLOATS)) 我想将这两个列表堆叠在一起,然后将它们以列的形式写入文本文件;因此,我想使用numpy.savetxt(如果可能)来执行此操作。 num.savetxt('test.txt', DAT, delimiter=" ") 当我这样做时,出现以下错误: >>> num.savetxt('test.txt', DAT, delimiter=" ") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Python/2.7/site-packages/numpy-1.8.0.dev_9597b1f_20120920-py2.7-macosx-10.8-x86_64.egg/numpy/lib/npyio.py", line 1047, in …
70 python  list  numpy  output 

17
Python切片列表中的第一个和最后一个元素
有没有办法只分割列表中的第一项和最后一项? 例如; 如果这是我的清单: >>> some_list ['1', 'B', '3', 'D', '5', 'F'] 我想这样做(显然[0,-1]是无效的语法): >>> first_item, last_item = some_list[0,-1] >>> print first_item '1' >>> print last_item 'F' 我尝试过的一些方法: In [3]: some_list[::-1] Out[3]: ['F', '5', 'D', '3', 'B', '1'] In [4]: some_list[-1:1:-1] Out[4]: ['F', '5', 'D', '3'] In [5]: some_list[0:-1:-1] Out[5]: [] ...
70 python  list  slice  subscript 

3
Django错误:render_to_response()获得了意外的关键字参数“ context_instance”
升级到Django 1.10后,出现错误render_to_response() got an unexpected keyword argument 'context_instance'。 我的看法如下: from django.shortcuts import render_to_response from django.template import RequestContext def my_view(request): context = {'foo': 'bar'} return render_to_response('my_template.html', context, context_instance=RequestContext(request)) 这是完整的回溯: Traceback: File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner 39. response = get_response(request) File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request) File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response …
70 python  django 



8
如何使用Python访问(读取和写入)Google表格电子表格?
我想知道您是否可以为我提供使用python从Google文档/电子表格中读取/写入的示例。 我确实在这里https://developers.google.com/google-apps/spreadsheets/上查看了google docs API,但不确定是否点击了正确的链接。一个例子也会有很大帮助。 我想做的是根据不同的列查询电子表格,更像是SQL查询,然后对数据进行一些下游解析,然后将其放入Google文档中的另一个电子表格或文档中。 最好-阿比

7
在python中生成HTML文档
在python中,生成HTML文档的最优雅的方法是什么。我目前将所有标签手动添加到一个巨大的字符串中,并将其写入文件中。有没有更优雅的方式做到这一点?
70 python  html 

2
打印python堆栈跟踪而无异常
我的类的实例变量之一正在发生某些事情。我想使变量成为一个属性,并且每当访问该变量时,我都希望打印出导致该点的所有代码的堆栈跟踪,因此我可以看到它的混乱之处。没有引发异常时,如何打印堆栈跟踪?我知道是否有例外,我可以做类似的事情traceback.format_tb(sys.exc_info()[2])。 同样有用的是仅打印最后3-4个级别,因为前几个级别可能不会那么有趣。

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.