Importing BitTorrent bencode module

I'm on Mac OS X 10.6. Python is 2.6.1. I've installed bencode module as sudo easy_install BitTorrent-bencode It appeared in site-packages /Library/Python/2.6/site-packages/BitTorrent_bencode-5.0.8-py2.6.egg But, how to import and use this module? >>> import bencode doesn't work... Traceback (most recent call last): File "<stdin>", line 1, in <module>

导入BitTorrent bencode模块

我在Mac OS X 10.6上。 Python是2.6.1。 我已经安装了bencode模块 sudo easy_install BitTorrent-bencode 它出现在网站包中 /Library/Python/2.6/site-packages/BitTorrent_bencode-5.0.8-py2.6.egg 但是,如何导入和使用这个模块? >>> import bencode 不起作用... Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named bencode 如何从

How to use threading in Python?

I am trying to understand threading in Python. I've looked at the documentation and examples, but quite frankly, many examples are overly sophisticated and I'm having trouble understanding them. How do you clearly show tasks being divided for multi-threading? Since this question was asked in 2010, there has been real simplification in how to do simple multithreading with python with

如何在Python中使用线程?

我想了解Python中的线程。 我已经看过文档和示例,但坦率地说,很多示例都非常复杂,我无法理解它们。 你如何清楚地显示被划分为多线程的任务? 自从2010年提出这个问题以来,已经真正简化了如何用map和pool进行python简单的多线程处理。 下面的代码来自一篇文章/博客文章,你一定要检查(无从属关系) - 并行性在一行中:日常线程任务的更好模型 。 我将在下面总结 - 它最终只是几行代码: from multiprocessing.dummy

How is calling module and function by string handled in python?

Calling a function of a module from a string with the function's name in Python shows us how to call a function by using getattr(" bar ")(), but this assumes that we have the module foo imported already. How would would we then go about calling for the execution of "foo.bar" assuming that we probably also have to perform the import of foo (or from bar import foo )? Use

在Python中如何通过字符串调用模块和函数?

在Python中使用带函数名称的字符串调用模块函数向我们展示了如何使用getattr(“ bar ”)()调用函数,但是这里假设我们已经导入了模块foo 。 那么假设我们可能还必须执行foo的导入(或者从bar导入foo ),那么我们会如何去调用“foo.bar”的执行? 使用__import__(....)函数: http://docs.python.org/library/functions.html# 进口 (大卫几乎拥有它,但我认为他的例子更适合于如果你想重新定义正常的导入过程 - 例如从

Delete an item from a dictionary

Is there a way to delete an item from a dictionary in Python? I know I can just call .pop on the dictionary, but that returns the item that was removed. What I'm looking for is something returns the dictionary minus the element in question. At present I have a helper function that accepts the dictionary in question as parameter, and then returns a dictionary with the item removed, Is the

从字典中删除项目

有没有办法从Python中的字典中删除项目? 我知道我可以在字典上调用.pop ,但是这会返回已删除的项目。 我正在寻找的是返回字典减去元素的问题。 目前,我有一个帮助函数,它接受有问题的字典作为参数,然后返回一个字典,删除该项目,是否有更优雅的解决方案? del语句删除一个元素: del d[key] 但是,这会改变现有的字典,因此字典的内容会更改为对同一个实例有引用的其他人。 要返回新字典,请复制字典: def remo

Determine function name from within that function (without using traceback)

In Python, without using the traceback module, is there a way to determine a function's name from within that function? Say I have a module foo with a function bar. When executing foo.bar() , is there a way for bar to know bar's name? Or better yet, foo.bar 's name? #foo.py def bar(): print "my name is", __myname__ # <== how do I calculate this at runtime? Python doesn&

从该函数中确定函数名称(不使用追溯)

在Python中,如果不使用traceback模块,有没有一种方法可以从该函数中确定函数的名称? 假设我有一个带功能栏的模块foo。 执行foo.bar() ,有没有一种方法可以让酒吧知道酒吧的名字? 或者更好, foo.bar的名字? #foo.py def bar(): print "my name is", __myname__ # <== how do I calculate this at runtime? Python没有功能来访问该函数本身内的函数或其名称。 它已被提出但被拒绝。 如果你不想自己玩堆叠

What is the best way to remove accents in a Python unicode string?

I have a Unicode string in Python, and I would like to remove all the accents (diacritics). I found on the Web an elegant way to do this in Java: convert the Unicode string to its long normalized form (with a separate character for letters and diacritics) remove all the characters whose Unicode type is "diacritic". Do I need to install a library such as pyICU or is this possible

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

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

Python list slice syntax used for no obvious reason

I occasionally see the list slice syntax used in Python code like this: newList = oldList[:] Surely this is just the same as: newList = oldList Or am I missing something? Like NXC said, Python variable names actually point to an object, and not a specific spot in memory. newList = oldList would create two different variables that point to the same object, therefore, changing oldList would

Python列表片语法没有明显的原因

我偶尔会看到Python代码中使用的列表切片语法,如下所示: newList = oldList[:] 这当然是一样的: newList = oldList 或者我错过了什么? 就像NXC说的那样,Python变量名称实际上指向一个对象,而不是内存中的特定位置。 newList = oldList会创建两个指向同一个对象的不同变量,因此,更改oldList也会更改newList 。 但是,当您执行newList = oldList[:] ,它会“切片”列表,并创建一个新列表。 [:]的默认值是0和列表

Regex or other way to check for word in variable?

This question already has an answer here: Does Python have a string 'contains' substring method? 13 answers All you have to do is change your ordering. You can use the operator 'in' to check if a string contains another string. if 'xyz' in 'oiurwernjfndj xyz iekjerk': print True You can probably just use in operator or str.find: https://docs.python.org/3/reference/expr

正则表达式或其他方式来检查变量中的单词?

这个问题在这里已经有了答案: Python是否有一个字符串'contains'substring方法? 13个答案 你所要做的就是改变你的订购。 您可以使用运算符'in'来检查一个字符串是否包含另一个字符串。 if 'xyz' in 'oiurwernjfndj xyz iekjerk': print True 您可能只需在运算符或str.find中使用:https://docs.python.org/3/reference/expressions.html#in https://docs.python.org/2/library/stdtypes.html?high

Single word search from CSV with multiple words in one cell

This question already has an answer here: Does Python have a string 'contains' substring method? 13 answers 除了解析csv文件之外,还可以查看所有列,并检查是否有一个字段包含关键字(您应该search in field使用search in field而不是==因为upsabraham指出),您还可以在此处保存一些处理能力,并检查关键字在该行的某处并按原样写入该行。 # -*- coding: utf-8 -*- in_file = 'TestDomstol.csv' out_file =

CSV中的单词搜索,在一个单元格中有多个单词

这个问题在这里已经有了答案: Python是否有一个字符串'contains'substring方法? 13个答案 除了解析csv文件之外,还可以查看所有列,并检查是否有一个字段包含关键字(您应该search in field使用search in field而不是==因为upsabraham指出),您还可以在此处保存一些处理能力,并检查关键字在该行的某处并按原样写入该行。 # -*- coding: utf-8 -*- in_file = 'TestDomstol.csv' out_file = 'verdictsOutput.csv'

How to find a string inside of a string space insensitive?

This question already has an answer here: Does Python have a string 'contains' substring method? 13 answers 简单: string = 'odfsdlkfn dskfThe Moonaosjfsl dflkfn' if 'The Moon' in string: dosomething you could use regular expressions: import re text = "odfsdlkfn dskfThe Moonaosjfsl dflkfn" if re.find("The Moon", text): ... and in this case, you could ingore casing with re(p

如何找到一个字符串空间内的字符串不敏感?

这个问题在这里已经有了答案: Python是否有一个字符串'contains'substring方法? 13个答案 简单: string = 'odfsdlkfn dskfThe Moonaosjfsl dflkfn' if 'The Moon' in string: dosomething 你可以使用正则表达式: import re text = "odfsdlkfn dskfThe Moonaosjfsl dflkfn" if re.find("The Moon", text): ... 在这种情况下,如果需要的话,你可以使用re(pattern, text, re.IGNORECASE) 。