我想在我的MySQL数据库中插入整数188和90,但下面的代码不起作用: import MySQLdb conn = MySQLdb.connect(host= "localhost", user="root", passwd="newpassword", db="engy1") x = conn.cursor() x.execute("SELECT * FROM anooog1") x.execute (" INSERT INTO anooog1 VALUES ('%s','%s') ", (188,90)) row = x.fetchall() 为什么不起作用?
我正在使用Python 2.6.5我想写一些日文字符到一个文件。 我得到这个错误,我不知道如何改变编码。 Python 2.6.5 (r265:79063, Jun 12 2010, 17:07:01) [GCC 4.3.4 20090804 (release) 1] on cygwin >>> s = u'\u5E73\u621015' >>> with open("yop", "wb") as f: … f.write( s + "\n" ); … Traceback (most recent call last): File "<stdin>", line 2, in <module> UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal […]
所以我有我的TagStatus模型。 我正在试图为它做一个ModelForm。 不过,我的表单要求用{{tag.name}}填充隐藏的input。 我一直在查看文档,我不知道如何使标签字段成为隐藏的input。 也许ModelForm不是要走的路? models.py: class TagStatus(models.Model): user = models.ForeignKey(User, null=True, unique=True) status = models.CharField(max_length=2, choices=tag_statuses) tag = models.ForeignKey(Tag, null=True, blank=True) def __unicode__(self): return self.status def save(self, *args, **kwargs): super(TagStatus, self).save(*args, **kwargs) class TagStatusForm(modelForm): class Meta: model = TagStatus fields = ('status','tag') widgets = { 'select': Select, 'tag': ??? } django views.py: @login_required def […]
我有一个str对象,例如:menu ='install'。 我想从这个string运行安装方法。 例如,当我调用菜单(一些参数),它会调用安装(一些参数)。 有没有办法做到这一点?
我有一个包含版本string的列表,例如: versions_list = ["1.1.2", "1.0.0", "1.3.3", "1.0.12", "1.0.2"] 我想sorting它,结果会是这样的: versions_list = ["1.0.0", "1.0.2", "1.0.12", "1.1.2", "1.3.3"] 数字的优先顺序显然应该是从左到右,应该是递减的。 所以1.2.3之前2.2.3和2.2.3之前都是2.2.3之前。 我如何在Python中做到这一点?
我有一个Python中的布尔值列表。 我想AND(或OR或NOT)他们并得到结果。 下面的代码工作,但不是pythonic。 def apply_and(alist): if len(alist) > 1: return alist[0] and apply_and(alist[1:]) else: return alist[0] 任何build议如何使其更pythonic赞赏。
我正在使用Python 2.7,如果我尝试安装Matplotlib我得到这个错误,如果我使用“pip安装matplotlib” Exception: Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/pip/basecommand.py", line 232, in main status = self.run(options, args) File "/usr/local/lib/python2.7/dist-packages/pip/commands/install.py", line 339, in run requirement_set.prepare_files(finder) File "/usr/local/lib/python2.7/dist-packages/pip/req/req_set.py", line 355, in prepare_files do_download, session=self.session, File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 782, in unpack_url session, File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 667, in unpack_http_url from_path, content_type = _download_http_url(link, session, temp_dir) File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line […]
我正在一个倒排索引的search程序。 索引本身就是一个字典,它的键是词,它们的值本身是短文档的字典,ID号是键,文本内容是值。 为了执行两个术语的“AND”search,因此我需要交叉他们的发布列表(字典)。 什么是明确的(不一定是非常聪明的)在Python中做到这一点? 我开始尝试了很长的路与iter : p1 = index[term1] p2 = index[term2] i1 = iter(p1) i2 = iter(p2) while … # not sure of the 'iter != end 'syntax in this case …
我正在写一个脚本来导入一些模型对象到我的Django应用程序使用的数据库中。 在过去,我通过运行./manage.py shell然后import myscript来解决这个问题。 我确定有更好的方法。 我希望能够使用python scriptname.py从我的HD上的任何地方调用脚本,并且在该脚本的前几行中,它将执行任何必需的导入/其他操作,以便它可以访问模型对象并像尽pipe它是使用manage.py shell运行的。 我需要添加到我的脚本来实现这一目标? 编辑: 根据@ Melug的回答,除了dynamic设置Pythonpath来解决“我的HD上的任何地方”部分的问题: import sys sys.path.append('c:\\my_projec_src_folder') from myproject import settings from django.core.management import setup_environ setup_environ(settings)
我正在尝试为我的领域中的一些stream行的Fortran代码做一个python包分发。 我希望它使用setup.py文件最标准的方法。 相关问题有助于学习如何打包Fortran扩展 。 当使用这种方法时,我注意到混合setuptools和numpy.distutils时有一些混淆的行为。 混合两者是不好的做法? 截至2015年,似乎最好尽可能使用setuptools 。 不过,我想用与numpy.兼容的方式构buildFortran扩展numpy. 所以我想从numpy.distutils导入来获得Extension和setup 。 我正在使用以下基本方法: from setuptools.command.develop import develop from numpy.distutils.core import Extension, setup ext_modules=[Extension("my_package.fortran_mod", sources=['src/fortran_mod.f'])] class MyDevelop(develop): def run(self): my_script() develop.run(self) setup( … ext_modules=ext_modules, cmdclass={'develop':MyDevelop}) 这似乎工作,但我有问题。 混合setuptools和numpy.distribute通常是一个好的做法吗? 我input的订单是否重要? 我应该总是先导入setuptools ? 有没有官方的最新教程打包numpy扩展? 也许甚至有一些讨论Fortran扩展? – 一些链接 http://www.fortran90.org/src/best-practices.html#interfacing-with-python