Questions tagged «python»

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


7
如何在Python中启动后台进程?
我正在尝试将Shell脚本移植到可读性更高的python版本。原始的shell脚本在后台使用“&”启动多个进程(实用程序,监视器等)。如何在python中达到相同的效果?我希望这些过程在Python脚本完成后不会消失。我敢肯定它与守护程序的概念有关,但是我找不到如何轻松实现此目的。
295 python  process  daemon 

12
检查给定键是否已存在于字典中并递增
给定字典,我如何找出该字典中的给定键是否已设置为非值? 即,我想这样做: my_dict = {} if (my_dict[key] != None): my_dict[key] = 1 else: my_dict[key] += 1 即,如果要已有一个,我想增加该值,否则,请将该值设置为1。
294 python  dictionary 



10
如何更正TypeError:散列之前必须对Unicode对象进行编码?
我有这个错误: Traceback (most recent call last): File "python_md5_cracker.py", line 27, in <module> m.update(line) TypeError: Unicode-objects must be encoded before hashing 当我尝试在Python 3.2.2中执行以下代码时: import hashlib, sys m = hashlib.md5() hash = "" hash_file = input("What is the file name in which the hash resides? ") wordlist = input("What is your wordlist? …

4
使用Python'with'语句时捕获异常
令我感到羞耻的是,我不知道如何处理python'with'语句的异常。如果我有代码: with open("a.txt") as f: print f.readlines() 我真的很想处理“找不到文件异常”以便执行某些操作。但是我不能写 with open("a.txt") as f: print f.readlines() except: print 'oops' 而且不能写 with open("a.txt") as f: print f.readlines() else: print 'oops' 在try / except语句中包含“ with”,否则将不起作用:不会引发异常。为了以Python方式处理“ with”语句中的失败,我该怎么办?

3
如何在Django queryset中执行OR条件?
我想编写一个与此SQL查询等效的Django查询: SELECT * from user where income >= 5000 or income is NULL. 如何构造Django queryset过滤器? User.objects.filter(income__gte=5000, income=0) 这是行不通的,因为它AND是过滤器。我想要OR过滤器以获取单个查询集的并集。


16
如何在NumPy数组中添加额外的列
假设我有一个NumPy数组a: a = np.array([ [1, 2, 3], [2, 3, 4] ]) 我想添加一列零以获取一个数组b: b = np.array([ [1, 2, 3, 0], [2, 3, 4, 0] ]) 我如何在NumPy中轻松做到这一点?
292 python  numpy 

13
在Python中搜索并替换文件中的一行
我想遍历文本文件的内容,进行搜索并替换某些行,然后将结果写回到文件中。我可以先将整个文件加载到内存中,然后再写回去,但这可能不是最好的方法。 在以下代码中,执行此操作的最佳方法是什么? f = open(file) for line in f: if line.contains('foo'): newline = line.replace('foo', 'bar') # how to write this newline back to the file
292 python  file 

9
如何正确断言pytest中引发了异常?
码: # coding=utf-8 import pytest def whatever(): return 9/0 def test_whatever(): try: whatever() except ZeroDivisionError as exc: pytest.fail(exc, pytrace=True) 输出: ================================ test session starts ================================= platform linux2 -- Python 2.7.3 -- py-1.4.20 -- pytest-2.5.2 plugins: django, cov collected 1 items pytest_test.py F ====================================== FAILURES ====================================== ___________________________________ test_whatever ____________________________________ def test_whatever(): …

9
将不同类型的项目列表作为字符串加入Python
我需要加入项目清单。列表中的许多项目都是从函数返回的整数值;即 myList.append(munfunc()) 如何将返回的结果转换为字符串,以便将其与列表连接? 我是否需要对每个整数值执行以下操作: myList.append(str(myfunc())) 有没有更Python化的方法来解决铸造问题?
292 python  string  list 

4
Django ORM中的select_related和prefetch_related有什么区别?
在Django文件中, select_related() “遵循”外键关系,在执行查询时选择其他相关对象数据。 prefetch_related() 对每个关系进行单独的查找,并在Python中执行“联接”。 “在python中进行连接”是什么意思?有人可以举例说明吗? 我的理解是,对于外键关系,使用select_related; 对于M2M关系,请使用prefetch_related。它是否正确?

22
我如何看待Python对象内部?
我开始使用Python在各种项目中进行编码(包括Django Web开发和Panda3D游戏开发)。 为了帮助我理解发生了什么,我想基本上在Python对象内部“看”看它们如何打勾-像它们的方法和属性一样。 假设我有一个Python对象,我需要打印出什么内容?那有可能吗?

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.