Questions tagged «python»

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

8
TypeError:method()接受1个位置参数,但给出了2个
如果我有课... class MyClass: def method(arg): print(arg) ...我用来创建对象的... my_object = MyClass() 我这样称呼method("foo")... >>> my_object.method("foo") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: method() takes exactly 1 positional argument (2 given) ...为什么当我只给出一个参数时,Python告诉我给它两个参数?

25
在Python中从字符串中剥离HTML
from mechanize import Browser br = Browser() br.open('http://somewebpage') html = br.response().readlines() for line in html: print line 当在HTML文件中打印一行时,我试图找到一种仅显示每个HTML元素的内容而不显示格式本身的方法。如果找到'<a href="whatever.com">some text</a>',它将仅打印“某些文本”,'<b>hello</b>'打印“ hello”,等等。如何做到这一点?
268 python  html 


16
在Python中按空格分隔字符串-保留带引号的子字符串
我有一个像这样的字符串: this is "a test" 我正在尝试在Python中编写一些内容,以按空格将其拆分,同时忽略引号内的空格。我正在寻找的结果是: ['this','is','a test'] PS。我知道您会问:“如果引号内有引号,将会发生什么情况?在我的应用程序中,那将永远不会发生。
268 python  regex 

7
TypeError:'str'不支持缓冲区接口
plaintext = input("Please enter the text you want to compress") filename = input("Please enter the desired filename") with gzip.open(filename + ".gz", "wb") as outfile: outfile.write(plaintext) 上面的python代码给了我以下错误: Traceback (most recent call last): File "C:/Users/Ankur Gupta/Desktop/Python_works/gzip_work1.py", line 33, in <module> compress_string() File "C:/Users/Ankur Gupta/Desktop/Python_works/gzip_work1.py", line 15, in compress_string outfile.write(plaintext) File "C:\Python32\lib\gzip.py", line …
267 python  string  gzip 





12
如何禁用Pylint警告?
我正在尝试在Pylint 0.21.1中禁用警告C0321(“在一行上有多个语句” –我经常将if具有短单行结果的语句放在同一行上)(如果重要:astng 0.20)。 1,常见的0.50.3,Python 2.6.6(r266:84292,2010年9月15日,16:22:56)。 我尝试添加disable=C0321Pylint配置文件,但是Pylint坚持要报告它。该行的变化(例如disable=0321或disable=C321)被标记为错误,因此Pylint 确实正确识别了该选项,只是忽略了它。 这是Pylint的错误,还是我做错了什么?有没有办法解决?我真的很想摆脱一些杂音。
266 python  pylint 

10
结合FOR循环和IF语句的Python方法
我知道如何在单独的行上同时使用for循环和if语句,例如: >>> a = [2,3,4,5,6,7,8,9,0] ... xyz = [0,12,4,6,242,7,9] ... for x in xyz: ... if x in a: ... print(x) 0,4,6,7,9 而且我知道当语句很简单时,我可以使用列表推导来组合这些内容,例如: print([x for x in xyz if x in a]) 但是,我找不到任何地方(复制和学习)的好例子,展示了一组复杂的命令(不仅是“ print x”),这些命令是在for循环和某些if语句组合后发生的。我期望的是: for x in xyz if x not in a: print(x...) 难道这不是python应该工作的方式吗?

5
Python,Matplotlib,子图:如何设置轴范围?
如何将第二个子图的y轴范围设置为[0,1000]?我的数据(文本文件中的一列)的FFT图导致一个(inf。?)尖峰,因此实际数据不可见。 pylab.ylim([0,1000]) 不幸的是,它没有任何作用。这是整个脚本: # based on http://www.swharden.com/blog/2009-01-21-signal-filtering-with-python/ import numpy, scipy, pylab, random xs = [] rawsignal = [] with open("test.dat", 'r') as f: for line in f: if line[0] != '#' and len(line) > 0: xs.append( int( line.split()[0] ) ) rawsignal.append( int( line.split()[1] ) ) h, w = 3, 1 …
266 python  range  axis  matplotlib 


6
解码Python字符串中的HTML实体?
我正在使用Beautiful Soup 3解析一些HTML,但是它包含HTML实体,Beautiful Soup 3不会自动为我解码: >>> from BeautifulSoup import BeautifulSoup >>> soup = BeautifulSoup("<p>£682m</p>") >>> text = soup.find("p").string >>> print text £682m 如何解码HTML实体text以获得"£682m"而不是"£682m"。

8
在if语句中初始化的变量的作用域是什么?
我是Python的新手,所以这可能是一个简单的范围界定问题。Python文件(模块)中的以下代码使我有些困惑: if __name__ == '__main__': x = 1 print x 在我使用过的其他语言中,此代码将引发异常,因为该x变量是if语句的局部变量,不应在其外部存在。但是此代码将执行并打印1。任何人都可以解释此行为吗?是否在模块中创建的所有变量都是全局的/可用于整个模块?

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.