Questions tagged «python»

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


8
ImportError:没有名为BeautifulSoup的模块
我已经使用easy_install安装了BeautifulSoup,并尝试运行以下脚本 from BeautifulSoup import BeautifulSoup import re doc = ['<html><head><title>Page title</title></head>', '<body><p id="firstpara" align="center">This is paragraph <b>one</b>.', '<p id="secondpara" align="blah">This is paragraph <b>two</b>.', '</html>'] soup = BeautifulSoup(''.join(doc)) print soup.prettify() 但不确定为什么会这样 Traceback (most recent call last): File "C:\Python27\reading and writing xml file from web1.py", line 49, in <module> from BeautifulSoup import …

10
将文档字符串添加到namedtuples?
是否可以通过简单的方式将文档字符串添加到namedtuple? 我试过了 from collections import namedtuple Point = namedtuple("Point", ["x", "y"]) """ A point in 2D space """ # Yet another test """ A(nother) point in 2D space """ Point2 = namedtuple("Point2", ["x", "y"]) print Point.__doc__ # -> "Point(x, y)" print Point2.__doc__ # -> "Point2(x, y)" 但这并不能解决问题。是否可以通过其他方式进行?

7
Python中不带[]的列表理解
加入清单: >>> ''.join([ str(_) for _ in xrange(10) ]) '0123456789' join 必须采取迭代。 显然,join的论点是[ str(_) for _ in xrange(10) ],这是一个列表理解。 看这个: >>>''.join( str(_) for _ in xrange(10) ) '0123456789' 现在,join的参数为just str(_) for _ in xrange(10),no [],但结果相同。 为什么?是否str(_) for _ in xrange(10)还会产生列表或可迭代项?

3
Python模块的绝对和显式相对导入
我想知道在Python应用程序中导入包的首选方法。我有一个这样的包结构: project.app1.models project.app1.views project.app2.models project.app1.views进口project.app1.models和project.app2.models。我想到有两种方法可以做到这一点。 绝对进口: import A.A import A.B.B 或具有明确的相对导入,如在Python 2.5中使用PEP 328引入的: # explicit relative from .. import A from . import B 什么是最pythonic的方式做到这一点?



8
在Tkinter中交互式验证Entry小部件内容
交互验证tkinterEntry小部件中的内容的推荐技术是什么? 我已经阅读了有关使用validate=True和的文章validatecommand=command,并且看来这些功能受到以下事实的限制:如果validatecommand命令更新了Entry小部件的值,这些功能将被清除。 鉴于这种行为,我们应该绑定的KeyPress,Cut以及Paste事件和监视/更新我们的Entry小部件的价值,通过这件事情?(还有我可能错过的其他相关事件?) 还是我们应该完全忘记交互式验证,而只对FocusOut事件进行验证?


2
处理后向引用以re.sub替换模式捕获组
我想取字符串0.71331, 52.25378并返回0.71331,52.25378-即只寻找一个数字,一个逗号,一个空格和一个数字,然后去掉空格。 这是我当前的代码: coords = '0.71331, 52.25378' coord_re = re.sub("(\d), (\d)", "\1,\2", coords) print coord_re 但这给了我0.7133,2.25378。我究竟做错了什么?
85 python  regex 

5
从JavaScript代码调用Python函数
我想从JavaScript代码中调用Python函数,因为JavaScript中没有其他方法可以做我想做的事情。这可能吗?您可以调整以下代码段使其正常工作吗? JavaScript代码: var tag = document.getElementsByTagName("p")[0]; text = tag.innerHTML; // Here I would like to call the Python interpreter with Python function arrOfStrings = openSomehowPythonInterpreter("~/pythoncode.py", "processParagraph(text)"); ~/pythoncode.py 包含使用高级库编写的函数,这些函数在JavaScript中没有易写的等效项: import nltk # is not in JavaScript def processParagraph(text): ... nltk calls ... return lst # returns a list of strings (will …

2
如何在Seaborn Lmplot FacetGrid中设置一些xlim和ylim
我正在使用Seaborn的lmplot绘制线性回归,将我的数据集分为两个类别变量。 对于x和y,我想手动设置两个图的下限,但将上界保留为Seaborn默认值。这是一个简单的例子: import pandas as pd import seaborn as sns import random n = 200 random.seed(2014) base_x = [random.random() for i in range(n)] base_y = [2*i for i in base_x] errors = [random.uniform(0,1) for i in range(n)] y = [i+j for i,j in zip(base_y,errors)] df = pd.DataFrame({'X': base_x, 'Y': y, …
85 python  pandas  seaborn 


6
熊猫中merge()和concat()之间的差异
pd.DataFrame.merge()和之间的本质区别是pd.concat()什么? 到目前为止,这是我发现的,请评论我的理解是多么完整和准确: .merge()只能使用列(加上行索引),并且在语义上适合于数据库样式的操作。.concat()可以与任一轴一起使用,仅使用索引,并提供添加分层索引的选项。 顺便提及,这允许以下冗余:两者都可以使用行索引组合两个数据帧。 pd.DataFrame.join() 只是为以下用例的一部分提供了简写 .merge() (Pandas非常擅长解决数据分析中的各种用例。探索文档以找出执行特定任务的最佳方法可能有些艰巨。)
85 python  pandas  join  merge  concat 

16
打印到同一行而不是python中的新行
基本上,我想做的和这个家伙相反。 Python脚本:每次将新行打印到外壳程序,而不是更新现有行 我有一个程序,告诉我它有多远。 for i in some_list: #do a bunch of stuff. print i/len(some_list)*100," percent complete" 因此,如果len(some_list)为50,我将最后一行打印50遍。我想打印一行并继续更新该行。我知道我知道这可能是您整天都会阅读的最棘手的问题。我只是想不通我需要输入到Google以获得答案的四个词。 更新!我尝试了mvds的建议,认为正确。新密码 print percent_complete," \r", 完成百分比只是一个字符串(我是第一次尝试抽象,现在是抽象的)。现在的结果是它运行该程序,直到程序结束后才打印任何内容,然后仅在一行上打印“ 100%完成”。 没有回车符(但逗号,是mvds建议的一半),直到最后都不会打印。然后打印: 0 percent complete 2 percent complete 3 percent complete 4 percent complete 等等。因此,现在的新问题是,直到程序完成,逗号才会打印出来。 使用回车符且没有逗号时,其行为与未使用逗号完全相同。
85 python  stdout 

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.