• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python编程常见问题整理【三】

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

编者按: 本文从stackoverflow收集了Python编程中的常见问题。基于google/baidu/bing翻译将问题议成了中文,希望在英语表达不地道(特别是中英文夹杂)的情况下,也能检索到优质内容入口。     Python相关问题非常多,我们会陆续将这些问题做成专辑,分成多篇文章分别展现。本文是其中的第三篇内容。 注: 点击问题标题直达英文原版网站,点击 加速访问 ,可以通过本站加速器快速访问。


1. UnicodeEncodeError:’ascii’编解码器不能编码字符u’ xa0’在位置20:序数不在范围(128)[Python] (UnicodeEncodeError: ‘ascii’ codec can’t encode character u’xa0′ in position 20: ordinal not in range(128))

unicode,beautifulsoup,python-2.x,python-unicode

我有问题处理从不同的网页(在不同的网站)提取的文本的Unicode字符。我使用BeautifulSoup。问题是,错误并不总是…

2. 解码Python字符串中的HTML实体?[Python] (Decode HTML entities in Python string?)

html,html-entities

我解析一些HTML美丽汤3,但它包含美丽的汤3不自动解码为我的HTML实体:>>> from BeautifulSoup import BeautifulSoup >>&…

3. 在Python中的动态模块导入[Python] (Dynamic module import in Python)

python-import

我编写一个Python应用程序作为一个命令作为参数,例如:$ python myapp.py command1我想要应用程序是可扩展的,也就是说,能够添加新的模块…

4. 如何使用subprocess.Popen通过管道连接多个进程?[Python] (How do I use subprocess.Popen to connect multiple processes by pipes?)

pipe,subprocess

如何使用Python子进程模块执行以下shell命令:echo“input data”| awk -f script.awk | sort> outfile.txt输入数据将来自一个字符串,所以我不…

5. 为什么Python的list.append求值为false?[Python] (Why does Python’s list.append evaluate to false?)

python

有一个原因是list.append评估为false?或者是它只是C成功返回0的成功,进入游戏?>>> u = [] >>> not u.append(6)True

6. Tkinter:AttributeError:NoneType对象没有属性get[Python] (Tkinter: AttributeError: NoneType object has no attribute get)

user-interface,tkinter

我看到一些类似的错误消息的其他帖子,但找不到一个解决方案,它会解决它在我的case.I使用TkInter一点点,并创建了一个非常简单的用户界面。代码如下:

7. 使用不同的Python版本与virtualenv[Python] (Use different Python version with virtualenv)

virtualenv,virtualenvwrapper

我有一个Debian系统目前运行与python 2.5.4。我得到virtualenv正确安装,一切工作正常。有可能,我可以使用virtualenv与不同的版本…

8. 在Python中反转字符串[Python] (Reverse a string in Python)

string

Python的str对象中没有内置的反向函数。什么是最好的实现方法?如果提供一个非常简洁的答案,请详细说明它的效率。是str转换…

9. 推荐使用哪种Python内存分析器? [关闭][Python] (Which Python memory profiler is recommended? [closed])

performance,memory-management,profiling

我想知道我的Python应用程序的内存使用,特别想知道什么代码块/部分或对象消耗最多的内存.Google搜索显示一个商业的是Python …

10. 为什么人们在Python脚本的第一行写#!/ usr / bin / env python?[Python] (Why do people write #!/usr/bin/env python on the first line of a Python script?)

shell,shebang

它似乎在我喜欢的文件运行相同没有这行。

11. 向现有对象实例添加方法[Python] (Adding a Method to an Existing Object Instance)

oop,methods,monkeypatching

我已经阅读,可以在Python中向现有对象添加一个方法(例如不在类定义中)。我明白,这并不总是好。但是,怎么可能这样做呢?

12. 如何检查字符串是否是Python中的数字(float)?[Python] (How do I check if a string is a number (float) in Python?)

casting,floating-point,type-conversion

什么是最好的方法来检查一个字符串是否可以表示为一个数字在Python?我现在的功能现在是:def is_number(s):try:float(s)return …

13. Python中单个下划线“_”变量的用途是什么?[Python] (What is the purpose of the single underscore “_” variable in Python?)

variables,naming-conventions,underscores,metasyntactic-variable

在这段代码中,_ after的含义是什么?if tbh.bag:n = 0 for _ in tbh.bag.atom_set():n + = 1

14. 什么是__init__.py?[Python] (What is __init__.py for?)

module

在Python源目录中是什么__init__.py?

15. 移除列表中的重复项[Python] (Removing duplicates in lists)

algorithm,list,duplicates,intersection

很多,我需要编写一个程序来检查一个列表是否有任何重复,如果它删除它们,并返回一个新的列表与不重复/删除的项目。这是我有,但…

16. 如何在Flask中提供静态文件[Python] (How to serve static files in Flask)

flask,static-files

所以这是尴尬。我有一个应用程序,我一起在Flask,现在它只是提供一个静态HTML页面与一些链接到CSS和JS。我找不到在哪里…

17. 超时对函数调用[Python] (Timeout on a function call)

multithreading,timeout,python-multithreading

我在Python中调用一个函数,我知道可能会停止,并强迫我重新启动脚本。如何调用函数或我将其包装在什么,以便如果它花费超过5秒的脚本…

18. Python:类和实例属性之间的区别[Python] (Python: Difference between class and instance attributes)

attributes

有什么有意义的区别:类A(对象):foo = 5#一些默认值vs.class B(对象):def __init __(self,foo = 5):self.foo = foo如果你创建了很多.. 。

19. Python部门[Python] (Python division)

math,python-2.x

我试图规范化一组数字从-100到0到10-100的范围,有问题,只有注意到,即使没有变量,这不评估我的期望…

20. 如何通过Python中字典的值对字典列表排序?[Python] (How do I sort a list of dictionaries by values of the dictionary in Python?)

list,sorting,dictionary

我得到一个字典列表,并希望按该字典的值排序。这[{‘name’:’Homer’,’age’:39},{‘name’:’Bart’,’age’ 10}]按名称排序,应该成为[{‘name’:’Bart’,’…

21. 你如何从Python中读取stdin?[Python] (How do you read from stdin in Python?)

stdin

我试图做一些代码高尔夫挑战,但他们都需要输入从stdin。如何在Python中得到它?

22. 使用Python解析JSON文件中的值?[Python] (Parsing values from a JSON file using Python?)

json,parsing

我有一个文件中的这个JSON:{“maps”:[{“id”:“blabla”,“iscategorical:”0“},{”id“:”blabla“,”iscategorical“:”0“。 。

23. 了解Python中的kwargs[Python] (Understanding kwargs in Python)

kwargs

在Python中** kwargs的用途是什么?我知道你可以在表上做一个objects.filter并传递一个** kwargs参数。我也可以这样做来指定时间delta,例如timedelta(hours = …

24. Python的隐藏特性[关闭][Python] (Hidden features of Python [closed])

hidden-features

Python程序设计语言中不太知名但有用的特性是什么?试着限制Python核心的答案。每个答案的一个特性。给出一个例子和简短的功能说明,…

25. 在Python中将字符串拆分为一个列表[Python] (Split string into a list in Python)

list,split,text-segmentation

我想我的Python函数分裂一个句子(输入),并将每个词存储在一个列表中。我到目前为止编写的代码分割了句子,但不将这些单词存储为列表。我怎么做?…

26. 如何使用Python通过HTTP下载文件?[Python] (How do I download a file over HTTP using Python?)

python

我有一个小实用程序,我用来从网站下载一个MP3的时间表,然后建立/更新podcast XML文件,我明显添加到iTunes。文本处理创建/ …

27. Python使用__new__和__init__?[Python] (Python’s use of __new__ and __init__?)

design-patterns,class-design

我只是想简化我的一个类,并引入了一些功能与flyweight设计模式相同的样式。然而,我有点困惑为什么__init__总是…

28. 解析字符串到浮点型或整数[Python] (Parse String to Float or Int)

string,parsing,floating-point,integer

在Python中,我如何解析一个类似“545.2222”的数字字符串到其对应的浮点值542.2222?或者解析字符串“31”到一个整数,31?我只是想知道如何解析一个浮点字符串…

29. 如何卸载(重新加载)一个Python模块?[Python] (How do I unload (reload) a Python module?)

module,reload,python-import

我有一个长期运行的Python服务器,并希望能够升级服务,而无需重新启动服务器。什么是最好的方式做这个?如果foo.py已更改:unimport foo < – How …

30. 如何获得列表元素的所有可能组合?[Python] (How to get all possible combinations of a list’s elements?)

combinations

我有一个15个数字的列表,我需要写一些代码,生成所有32,768组合的数字。我发现一些代码(通过谷歌),显然是做我想要的,…

31. 评估字符串中的数学表达式[Python] (Evaluating a mathematical expression in a string)

math

stringExp =“2 ^ 4”intVal = int(stringExp)#预期值:16这将返回以下错误:Traceback(最近一次调用):文件“<stdin>

32. IPython Notebook语言环境错误[重复][Python] (IPython Notebook locale error [duplicate])

python-2.7,locale,ipython,ipython-notebook

安装最新的Mac OSX 64位Anaconda Python发行版后,我在尝试启动IPython Notebook时仍然收到一个ValueError。启动ipython工作正常:3-millerc-〜:ipython …

33. 查找数据时间之间是否已经过24小时 – Python[Python] (Find if 24 hrs have passed between datetimes – Python)

datetime,timezone

我有以下方法:#last_updated是一个datetime()对象,表示上次这个程序randef time_diff(last_updated):day_period = last_updated.replace(day = last_updated.day + 1,…

34. 如何在Python中使用线程?[Python] (How to use threading in Python?)

multithreading

我试图理解Python中的线程。我看了文档和例子,但坦率地说,许多例子是过于复杂,我有麻烦理解他们。如何…

35. 在Python中滚动或滑动窗口迭代器[Python] (Rolling or sliding window iterator in Python)

algorithm

我需要一个滚动窗口(aka滑动窗口)可迭代在序列/迭代器/生成器。默认Python迭代可以认为是一种特殊情况,其中窗口长度为1.我目前正在使用…

36. 在Python’for’循环中访问索引[Python] (Accessing the index in Python ‘for’ loops)

loops,list

我如何访问索引本身为一个列表,如下列?ints = [8,23,45,12,78]当我使用for循环通过它,如何访问循环索引,从1到5这个案例?

37. python中isinstance()和type()之间的区别[Python] (Differences between isinstance() and type() in python)

oop,inheritance,types

这两个代码片段之间有什么区别?使用type():import typesif type(a)is types.DictType:do_something()if type(b)in …

38. 在Python中循环(或循环)导入[Python] (Circular (or cyclic) imports in Python)

circular-dependency,cyclic-reference

如果两个模块导入对方会发生什么?要概括问题,Python中的循环导入?

39. 列表和元组之间有什么区别?[Python] (What’s the difference between lists and tuples?)

list,tuples

有什么区别?元组/列表的优点/缺点是什么?

40. 将两个列表映射到Python中的字典[Python] (Map two lists into a dictionary in Python)

dictionary

想象一下,你有:keys =(’name’,’age’,’food’)values =(’Monty’,42,’spam’)什么是最简单的方法来产生下面的字典:a_dict = {‘name’ :’monty’,’age’:42,’food’:’…

41. append和extend[Python] (append vs. extend)

list,append,extend

列表方法append()和extend()之间有什么区别?

42. 在Python中创建一个带有列表解析的字典[Python] (Create a dictionary with list comprehension in Python)

dictionary,list-comprehension,language-features,dict-comprehension

我喜欢Python列表解析语法。它可以用于创建字典吗?例如,通过对成对的键和值进行迭代:mydict = {(k,v)for(k,v)in blah blah blah}#doesn’…

43. Python的super()如何使用多继承?[Python] (How does Python’s super() work with multiple inheritance?)

multiple-inheritance

我在Python面向对象编程中几乎是新的,我有麻烦理解super()函数(新样式类),特别是当涉及到多重继承。例如如果…

44. 什么是python“with”语句设计?[Python] (What is the python “with” statement designed for?)

language-features,with-statement

我今天第一次遇到了带有语句的Python。我一直使用Python几个月,甚至不知道它的存在!鉴于它有点模糊的地位,我…

45. 如何使用Python观察文件以进行更改?[Python] (How do I watch a file for changes using Python?)

file,pywin32,watch

我有一个日志文件由另一个进程,我想要监视的更改。每次发生变化时,我想读取新的数据,对它做一些处理。什么是最好的方式…

46. 如何解析ISO 8601格式的日期?[Python] (How to parse an ISO 8601-formatted date?)

parsing,datetime,iso8601,rfc3339

我需要解析RFC 3339字符串像“2008-09-03T20:56:35.450686Z”到Python的datetime类型。我已经在Python标准库中找到strptime,但它不是很方便。什么是最好的…

47. 在numpy.array中查找唯一的行[Python] (Find unique rows in numpy.array)

arrays,numpy,unique

我需要在numpy.array中找到唯一的行。例如:>>> a#I hasarray([[1,1,1,0,0,0],[0,1,1,1,0,0 ],[0,1,1,0,0],[1,1,1,0,0,0],[…]

48. Python子进程readlines()挂起[Python] (Python subprocess readlines() hangs)

subprocess

我试图完成的任务是流一个ruby文件并打印输出。 (注意:我不想一次打印出所有东西)main.pyfrom subprocess import Popen,PIPE,STDOUTimport pty …

49. 限制浮动到两个小数点[Python] (Limiting floats to two decimal points)

floating-point,precision

我想要一个舍入到13.95。>>> a13.949999999999999 >>> round(a,2)13.949999999999999圆函数不工作的方式,我预期。

50. Python中的命令行参数[Python] (Command Line Arguments In Python)

command-line,command-line-arguments

我最初是一个C程序员。我看到了许多技巧和“黑客”阅读许多不同的论点。 Python程序员可以做些什么方式?RelatedWhat是最好的方式…


鲜花

握手

雷人

路过

鸡蛋
专题导读
上一篇:
揭开机器学习的面纱:SVM 100行代码实现[Python版]发布时间:2022-05-14
下一篇:
Python编程常见问题整理【四】发布时间:2022-05-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap