Tag: python

IPython笔记本区域设置错误

安装最新的Mac OSX 64位Anaconda Python发行版之后 ,在尝试启动IPython Notebook时,我不断收到一个ValueError。 开始ipython正常工作: 3-millerc-~:ipython Python 2.7.3 |Anaconda 1.4.0 (x86_64)| (default, Feb 25 2013, 18:45:56) Type "copyright", "credits" or "license" for more information. IPython 0.13.1 — An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about […]

通过“ElementTree”在Python中用命名空间parsingXML

我有我想用Python的ElementTreeparsing的以下XML: <rdf:RDF xml:base="http://dbpedia.org/ontology/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns="http://dbpedia.org/ontology/"> <owl:Class rdf:about="http://dbpedia.org/ontology/BasketballLeague"> <rdfs:label xml:lang="en">basketball league</rdfs:label> <rdfs:comment xml:lang="en"> a group of sports teams that compete against each other in Basketball </rdfs:comment> </owl:Class> </rdf:RDF> 我想查找所有owl:Class标签,然后提取其中所有rdfs:label实例的值。 我正在使用下面的代码: tree = ET.parse("filename") root = tree.getroot() root.findall('owl:Class') 由于命名空间,我收到以下错误。 SyntaxError: prefix 'owl' not found in prefix map 我尝试阅读文档在http://effbot.org/zone/element-namespaces.htm,但我仍然无法得到这个工作,因为上面的XML有多个嵌套的命名空间。 请让我知道如何改变代码来find所有的owl:Class标签。

在Python中,为什么函数可以修改调用者感觉到的一些参数,而不是其他的?

我是Python的新手,并试图理解它对variables作用域的方法。 在这个例子中,为什么f()能够改变在main()感知的x的值,而不是n的值? def f(n, x): n = 2 x.append(4) print 'In f():', n, x def main(): n = 1 x = [0,1,2,3] print 'Before:', n, x f(n, x) print 'After: ', n, x main() 输出: Before: 1 [0, 1, 2, 3] In f(): 2 [0, 1, 2, 3, 4] After: 1 [0, 1, 2, 3, […]

了解Python中的生成器

现在阅读Python食谱,目前正在查看发电机。 我很难find我的头。 因为我来自Java背景,有没有Java的等价物? 这本书讲的是“生产者/消费者”,但是当我听说我想到穿线。 任何人都可以解释一个发电机是什么,为什么你会使用它? 没有引用任何书籍,显然(除非你可以从一本书直接find一个体面的,简单的答案)。 也许举个例子,如果你感到慷慨!

删除Python unicodestring中的重音符号的最佳方法是什么?

我在Python中有一个Unicodestring,我想删除所有的重音符(变音符号)。 我在网上find了一个在Java中这样做的优雅方法: 将Unicodestring转换为其长规格化forms(使用单独的字母和变音符号) 删除Unicodetypes为“diacritic”的所有字符。 我是否需要安装一个库,比如pyICU,或者只用python标准库就可以吗? 那么python 3呢? 重要说明:我想避免代码显式地从重音字符映射到非重音字符。

用Python读大文件的懒惰方法?

我有一个非常大的文件4GB,当我尝试读取它我的电脑挂起。 所以我想一个一个地读一遍,每一个处理后的文件都存放在另一个文件中,然后读下一个文件。 有没有什么方法可以yield这些碎片? 我很想有一个懒惰的方法 。

如何在Python中便宜地计算行数?

我需要在python中获得大文件(数十万行)的行数。 什么是记忆和时间最有效的方式? 目前我这样做: def file_len(fname): with open(fname) as f: for i, l in enumerate(f): pass return i + 1 有没有可能做得更好?

如何解决“在非包中尝试相对导入”,即使使用了__init__.py

我试图按照以下目录结构遵循PEP 328 : pkg/ __init__.py components/ core.py __init__.py tests/ core_test.py __init__.py 在core_test.py我有以下的导入语句 from ..components.core import GameLoopEvents 但是,当我运行时,我得到以下错误: tests$ python core_test.py Traceback (most recent call last): File "core_test.py", line 3, in <module> from ..components.core import GameLoopEvents ValueError: Attempted relative import in non-package search我发现“ 相对path不工作,即使__init__.py ”和“ 从相对path导入模块 ”,但他们没有帮助。 有什么我在这里失踪?

Pythonsubprocessreadlines()挂起

我试图完成的任务是stream式传输一个ruby文件并打印输出。 ( 注意 :我不想一次打印所有内容) main.py from subprocess import Popen, PIPE, STDOUT import pty import os file_path = '/Users/luciano/Desktop/ruby_sleep.rb' command = ' '.join(["ruby", file_path]) master, slave = pty.openpty() proc = Popen(command, bufsize=0, shell=True, stdout=slave, stderr=slave, close_fds=True) stdout = os.fdopen(master, 'r', 0) while proc.poll() is None: data = stdout.readline() if data != "": print(data) else: break print("This […]

如何在Python中连接两个列表?

我如何在Python中连接两个列表? 例: listone = [1, 2, 3] listtwo = [4, 5, 6] 预期结果: joinedlist == [1, 2, 3, 4, 5, 6]