Questions tagged «python»

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



18
Python PIP安装会引发TypeError:-=:'Retry'和'int'不受支持的操作数类型
使用pip install了显然与蟒蛇2.7.11+我的Ubuntu 16.04系统中的任何模块抛出这个错误: TypeError: unsupported operand type(s) for -=: 'Retry' and 'int' 点子有什么问题?如有必要,如何重新安装? 更新:下面是完整的追溯 sunny@sunny:~$ pip install requests Collecting requests Exception: Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 209, in main status = self.run(options, args) File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 328, in run wb.build(autobuilding=True) File "/usr/lib/python2.7/dist-packages/pip/wheel.py", line 748, in build self.requirement_set.prepare_files(self.finder) …
106 python  pip 

6
将Python搜索路径扩展到其他来源
我刚刚加入了一个具有相当大的现有代码库的项目。我们用linux开发,不使用和IDE。我们通过命令行运行。我试图弄清楚如何在运行项目模块时让python搜索正确的路径。例如,当我运行类似的内容时: python someprojectfile.py 我懂了 ImportError: no module named core.'somemodule' 我认为所有导入的内容都是这样,我认为这是路径问题。 TLDR: 如何~/codez/project/在导入语句期间让Python搜索* .py文件以及所有文件和文件夹。
106 python  search  import  path 

8
使用Python将文本添加到现有PDF
我需要使用Python向现有的PDF中添加一些额外的文本,最好的方法是什么,需要安装哪些额外的模块。 注意:理想情况下,我希望能够在Windows和Linux上都可以运行此程序,但是一键式仅Linux可以运行。 编辑:pyPDF和ReportLab看起来不错,但没人允许我编辑现有的PDF,还有其他选择吗?
106 python  pdf 

2
TypeError:无法在re.findall()中的类似字节的对象上使用字符串模式
我正在尝试学习如何自动从页面获取URL。在下面的代码中,我试图获取网页的标题: import urllib.request import re url = "http://www.google.com" regex = r'<title>(,+?)</title>' pattern = re.compile(regex) with urllib.request.urlopen(url) as response: html = response.read() title = re.findall(pattern, html) print(title) 我得到这个意外的错误: Traceback (most recent call last): File "path\to\file\Crawler.py", line 11, in <module> title = re.findall(pattern, html) File "C:\Python33\lib\re.py", line 201, in findall return _compile(pattern, …

6
在Python的另一个类中定义一个类是否有好处?
我在这里谈论的是嵌套类。本质上,我有两个正在建模的类。一个DownloadManager类和一个DownloadThread类。显而易见的OOP概念是组合。但是,组合不一定意味着嵌套,对吗? 我有看起来像这样的代码: class DownloadThread: def foo(self): pass class DownloadManager(): def __init__(self): dwld_threads = [] def create_new_thread(): dwld_threads.append(DownloadThread()) 但是现在我想知道是否存在嵌套更好的情况。就像是: class DownloadManager(): class DownloadThread: def foo(self): pass def __init__(self): dwld_threads = [] def create_new_thread(): dwld_threads.append(DownloadManager.DownloadThread())
106 python  oop 

5
Python的字符串格式化的许多方式-较旧的(即将被淘汰)吗?
Python至少有六种格式化字符串的方式: In [1]: world = "Earth" # method 1a In [2]: "Hello, %s" % world Out[2]: 'Hello, Earth' # method 1b In [3]: "Hello, %(planet)s" % {"planet": world} Out[3]: 'Hello, Earth' # method 2a In [4]: "Hello, {0}".format(world) Out[4]: 'Hello, Earth' # method 2b In [5]: "Hello, {planet}".format(planet=world) Out[5]: 'Hello, …

11
使用virtualenv进行pip安装Matplotlib错误
我正在尝试在新的virtualenv中安装matplotlib。 当我做: pip install matplotlib 要么 pip install http://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-1.1.0/matplotlib-1.1.0.tar.gz 我收到此错误: building 'matplotlib._png' extension gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -fPIC - DPY_ARRAY_UNIQUE_SYMBOL=MPL_ARRAY_API -DPYCXX_ISO_CPP_LIB=1 -I/usr/local/include -I/usr/include -I. -I/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/numpy/core/include -I. -I/usr/include/python2.7 -c src/_png.cpp -o build/temp.linux-x86_64-2.7/src/_png.o src/_png.cpp:10:20: fatal error: png.h: No such file or directory compilation terminated. error: command 'gcc' failed …


16
UnicodeDecodeError:“ ascii”编解码器无法解码位置1的字节0xef
我在尝试将字符串编码为UTF-8时遇到一些问题。我已经尝试了很多事情,包括使用string.encode('utf-8')和unicode(string),但是出现错误: UnicodeDecodeError:'ascii'编解码器无法解码位置1的字节0xef:序数不在范围内(128) 这是我的字符串: (。・ω・。)ノ 我看不出怎么了,知道吗? 编辑:问题是按原样打印字符串无法正确显示。此外,当我尝试将其转换为此错误时: Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> s = '(\xef\xbd\xa1\xef\xbd\xa5\xcf\x89\xef\xbd\xa5\xef\xbd\xa1)\xef\xbe\x89' >>> s1 = s.decode('utf-8') >>> print s1 Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeEncodeError: 'ascii' …
106 python  unicode  utf-8 

6
使用Python 2.7.3在64位Windows 7上安装Numpy
关闭。此问题不符合堆栈溢出准则。它当前不接受答案。 想改善这个问题吗?更新问题,使其成为Stack Overflow 的主题。 4年前关闭。 改善这个问题 看起来Numpy的唯一64位Windows安装程序适用于Numpy版本1.3.0,仅适用于Python 2.6 http://sourceforge.net/projects/numpy/files/NumPy/ 我不得不回滚到Python 2.6才能在Windows上使用Numpy,这让我感到很奇怪,这让我觉得我缺少了一些东西。 是吗

16
如何将没有空格的文本分割成单词列表?
输入: "tableapplechairtablecupboard..."很多单词 将此类文本拆分为单词列表并获得以下内容的有效算法是什么? 输出: ["table", "apple", "chair", "table", ["cupboard", ["cup", "board"]], ...] 首先想到的是遍历所有可能的单词(从第一个字母开始)并找到可能的最长单词,然后从 position=word_position+len(word) PS: 我们列出了所有可能的单词。 单词“ cupboard”可以是“ cup”和“ board”,选择时间最长。 语言:python,但主要是算法本身。
106 python  algorithm  text  split 

10
替换Python中的控制台输出
我想知道如何像某些C / C ++程序那样在Python中创建这些漂亮的控制台计数器之一。 我在做一个循环,当前输出如下: Doing thing 0 Doing thing 1 Doing thing 2 ... 更整洁的是只更新最后一行; X things done. 我已经在许多控制台程序中看到了这一点,并且想知道是否/如何在Python中做到这一点。
106 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.