Tag: python

python:使用matplotlib使用字典绘制一个栏

有没有什么办法用matplotlib直接从字典中使用数据绘制条形图? 我的字典看起来像这样: D = {u'Label1':26, u'Label2': 17, u'Label3':30} 我期待着 fig = plt.figure(figsize=(5.5,3),dpi=300) ax = fig.add_subplot(111) bar = ax.bar(D,range(1,len(D)+1,1),0.5) 去工作,但事实并非如此。 这是错误: >>> ax.bar(D,range(1,len(D)+1,1),0.5) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/site-packages/matplotlib/axes.py", line 4904, in bar self.add_patch(r) File "/usr/local/lib/python2.7/site-packages/matplotlib/axes.py", line 1570, in add_patch self._update_patch_limits(p) File "/usr/local/lib/python2.7/site-packages/matplotlib/axes.py", line 1588, in _update_patch_limits xys = […]

Python检查string的第一个和最后一个字符

任何人都可以请解释这个代码有什么问题吗? str1='"xxx"' print str1 if str1[:1].startswith('"'): if str1[:-1].endswith('"'): print "hi" else: print "condition fails" else: print "bye" 我得到的输出是: Condition fails 但我希望它打印hi而不是。

如何使Tornado中的SQLAlchemy成为asynchronous?

如何使Tornado SQLAlchemy成为async ? 我在MongoDB上find了asynchronousmongo例子的例子,但是我找不到像SQLAlchemy这样的motor 。 有谁知道如何使SQLAlchemy查询执行与tornado.gen (我正在使用MySQL下面的SQLAlchemy ,目前我的处理程序读取数据库和返回结果,我想使这个asynchronous)。

在Windows上安装virtualenvwrapper

我使用easy_install在Windows上安装了virtualenv和virtualenvwrapper。 但mkvirtualenv失踪。 我试图在我的机器上search,但我找不到它。 我不知道如何解决它。 你有什么主意吗?

py.test:将一个parameter passing给一个fixture函数

我正在使用py.test来testing包装在python类MyTester中的一些DLL代码。 为了validation目的,我需要在testing过程中logging一些testing数据,然后再做更多的处理。 因为我有很多testing_…文件,我想在testing中重用testing器对象的创build(MyTester的实例)。 由于testing者对象是获得对DLL的variables和函数的引用的对象,因此我需要将每个testing文件的DLLvariables列表传递给testing者对象(要logging的variables对于testing是相同的。 。文件)。 列表的内容应该用来logging指定的数据。 我的想法是这样做: import pytest class MyTester(): def __init__(self, arg = ["var0", "var1"]): self.arg = arg # self.use_arg_to_init_logging_part() def dothis(self): print "this" def dothat(self): print "that" # located in conftest.py (because other test will reuse it) @pytest.fixture() def tester(request): """ create tester object """ # how to use the list below […]

如何为Python安装SimpleJson包

http://pypi.python.org/pypi/simplejson 我只是潜入Python世界,并希望做一个简单的Twitter应用程序,需要安装simplejson,但不知道如何设置它,并得到它的工作.. 我在一个Windows系统上

在C#中是否有相当于Pythons范围(12)?

这种情况对我来说是时不时的了:我有一些C#代码非常需要Python中可用的range()函数。 我知道使用 for (int i = 0; i < 12; i++) { // add code here } 但是这在function用法上刹车,就像我想要做一个Linq Sum()而不是写上面的循环一样。 有没有内build的? 我想我总是可以用自己的yield或者类似的东西来滚动自己的产品,但是这样做会非常方便。

在Python和sqlite中转义字符

我有一个python脚本读取原始电影文本文件到sqlite数据库。 我使用re.escape(title)在执行插入操作之前将转义字符添加到string中以使它们安全。 为什么这不起作用: In [16]: c.execute("UPDATE movies SET rating = '8.7' WHERE name='\'Allo\ \'Allo\!\"\ \(1982\)'") ————————————————————————— OperationalError Traceback (most recent call last) /home/rajat/Dropbox/amdb/<ipython console> in <module>() OperationalError: near "Allo": syntax error 然而,这工作(删除两个地方): In [17]: c.execute("UPDATE movies SET rating = '8.7' WHERE name='Allo\ Allo\!\"\ \(1982\)'") Out[17]: <sqlite3.Cursor object at 0x9666e90> 我无法弄清楚。 我也不能排除那些引号,因为他们实际上是电影片名的一部分。 谢谢。

python easy_install在Mac OS X上的“assembler for architecture ppc not installed”失败

bash-3.2$ sudo easy_install appscript Password: Searching for appscript Reading http://pypi.python.org/simple/appscript/ Reading http://appscript.sourceforge.net Best match: appscript 1.0.0 Downloading http://pypi.python.org/packages/source/a/appscript/appscript-1.0.0.tar.gz#md5=6619b637037ea0f391f45870c13ae38a Processing appscript-1.0.0.tar.gz Running appscript-1.0.0/setup.py -q bdist_egg –dist-dir /tmp/easy_install-C4d1az/appscript-1.0.0/egg-dist-tmp-yVTHww /usr/libexec/gcc/powerpc-apple-darwin10/4.2.1/as: assembler (/usr/bin/../libexec/gcc/darwin/ppc/as or /usr/bin/../local/libexec/gcc/darwin/ppc/as) for architecture ppc not installed Installed assemblers are: /usr/bin/../libexec/gcc/darwin/x86_64/as for architecture x86_64 /usr/bin/../libexec/gcc/darwin/i386/as for architecture i386 我是一个很大的noob在这个东西(我已经学会了使用python和unix了一下,但我从来没有处理安装。)早些时候,我得到一个错误,相关的gcc-4.2没有被发现,我发现一些build议重新安装XCode的post。 我去了4.0(不好的select?),现在我明白了。 我不知道该怎么做。

在Python中列表的第一个位置插入

如何在列表的第一个索引处插入一个元素? 如果我使用list.insert(0,elem),请elem修改第一个索引的内容? 或者,我必须创build一个新的列表与第一elem,然后复制在这个新的旧列表?