小编Kev*_*uan的帖子

如何摆脱BeautifulSoup用户警告?

安装BeautifulSoup之后,每当我在cmd中运行我的Python时,就会出现这个警告.

D:\Application\python\lib\site-packages\beautifulsoup4-4.4.1-py3.4.egg\bs4\__init__.py:166:
UserWarning: No parser was explicitly specified, so I'm using the best
available HTML parser for this system ("html.parser"). This usually isn't a
problem, but if you run this code on another system, or in a different
virtual environment, it may use a different parser and behave differently.

To get rid of this warning, change this:

 BeautifulSoup([your markup])

to this:

 BeautifulSoup([your markup], "html.parser")
Run Code Online (Sandbox Code Playgroud)

我没有理解为什么它出来以及如何解决它.

python beautifulsoup user-warning

41
推荐指数
4
解决办法
5万
查看次数

在Django中序列化外键对象

我一直致力于在Django中开发一些RESTful服务,以便与Flash和Android应用程序一起使用.

开发服务接口非常简单,但我遇到了序列化具有外键和多对多关系的对象的问题.

我有这样的模型:

class Artifact( models.Model ):
    name                = models.CharField( max_length = 255 )
    year_of_origin      = models.IntegerField( max_length = 4, blank = True, null = True )
    object_type         = models.ForeignKey( ObjectType, blank = True, null = True )
    individual          = models.ForeignKey( Individual, blank = True, null = True )
    notes               = models.TextField( blank = True, null = True )
Run Code Online (Sandbox Code Playgroud)

然后我将使用这样的模型执行查询select_related(),以确保遵循外键关系:

artifact = Artifact.objects.select_related().get(pk=pk)
Run Code Online (Sandbox Code Playgroud)

一旦我拥有了该对象,我将其序列化,并将其传递回我的视图:

serializers.serialize( "json", [ artifact ] )
Run Code Online (Sandbox Code Playgroud)

这就是我得到的,请注意外键(object_type和individual)只是其相关对象的id.

[
      {
            pk: 1
            model: "artifacts.artifact"
            fields: …
Run Code Online (Sandbox Code Playgroud)

python django django-orm

39
推荐指数
3
解决办法
3万
查看次数

在ubuntu 14.04上安装tkinter

我想在Ubuntu 14.04上运行python代码,但是当我执行它时,它会给我以下错误消息

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    from tkinter import *
ImportError: No module named tkinter
Run Code Online (Sandbox Code Playgroud)

python tkinter python-2.7 ubuntu-14.04

36
推荐指数
3
解决办法
13万
查看次数

Spark按照值排序集合

我正在尝试本教程http://spark.apache.org/docs/latest/quick-start.html 我首先从文件创建了一个集合

textFile = sc.textFile("README.md")
Run Code Online (Sandbox Code Playgroud)

然后我尝试了一个命令来解决这些问题:

wordCounts = textFile.flatMap(lambda line: line.split()).map(lambda word: (word, 1)).reduceByKey(lambda a, b: a+b)
Run Code Online (Sandbox Code Playgroud)

要打印集合:

 wordCounts.collect()
Run Code Online (Sandbox Code Playgroud)

我找到了如何使用命令sortByKey逐字排序.我想知道如何通过值进行排序可以做同样的事情,在这种情况下,文档中出现一个单词的数字.

sorting word-count apache-spark

35
推荐指数
6
解决办法
8万
查看次数

比较python中的两个文件报告差异

我有两个名为"hosts"的文件(在不同的目录中)

我想用python比较它们,看看它们是否是IDENTICAL.如果它们不相同,我想在屏幕上打印差异.

到目前为止,我已经尝试过了

hosts0 = open(dst1 + "/hosts","r") 
hosts1 = open(dst2 + "/hosts","r")

lines1 = hosts0.readlines()

for i,lines2 in enumerate(hosts1):
    if lines2 != lines1[i]:
        print "line ", i, " in hosts1 is different \n"
        print lines2
    else:
        print "same"
Run Code Online (Sandbox Code Playgroud)

但是当我跑这个时,我得到了

File "./audit.py", line 34, in <module>
  if lines2 != lines1[i]:
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)

这意味着其中一个主机拥有比另一个更多的线路.有没有更好的方法来比较2个文件并报告差异?

python comparison file

34
推荐指数
2
解决办法
7万
查看次数

在Python中将运算符赋值给变量?

将数学应用于变量的常用方法是

a * b
Run Code Online (Sandbox Code Playgroud)

它能够计算和操作这样的两个操作数吗?

a = input('enter a value')
b = input('enter a value') 
op = raw_input('enter a operand')
Run Code Online (Sandbox Code Playgroud)

那么如何连接OP和两个变量ab
我知道我可以比较运到+,-,%,$,然后分配和计算....

但我可以做一些事情a op b,如何告诉编译器op是一个运算符?

python compiler-construction operators

32
推荐指数
3
解决办法
2万
查看次数

在文本文件的指定位置插入行

我有一个文本文件,如下所示:

blah blah
foo1 bar1
foo1 bar2
foo1 bar3
foo2 bar4
foo2 bar5
blah blah
Run Code Online (Sandbox Code Playgroud)

现在我想'foo bar''foo1 bar3'和之间插入'foo2 bar4'.

我就这样做了:

import shutil

txt = '1.txt'
tmptxt = '1.txt.tmp'

with open(tmptxt, 'w') as outfile:
    with open(txt, 'r') as infile:
        flag = 0
        for line in infile:
            if not line.startswith('foo1') and flag == 0:
                outfile.write(line)
                continue
            if line.startswith('foo1') and flag == 0:
                flag = 1
                outfile.write(line)
                continue
            if line.startswith('foo1') and flag == 1:
                outfile.write(line)
                continue
            if …
Run Code Online (Sandbox Code Playgroud)

python text insert

31
推荐指数
3
解决办法
5万
查看次数

pip-installed uWSGI ./python_plugin.so错误

我已经使用pip安装了uWSGI并使用XML启动它来加载我的应用程序.XML配置包含<plugin>python</plugin>.在我的新服务器上,它会导致错误:

open("./python_plugin.so"): No such file or directory [core/utils.c line 3321]
!!! UNABLE to load uWSGI plugin: ./python_plugin.so: cannot open shared object file: No such file or directory !!!
Run Code Online (Sandbox Code Playgroud)

我可以找到.c.o版本:

sudo find / -name 'python_plugin.c'
/srv/www/li/venv/build/uwsgi/build/uwsgi/plugins/python/python_plugin.c
/srv/www/li/venv/build/uwsgi/plugins/python/python_plugin.c
sudo find / -name 'python_plugin.o'
/srv/www/li/venv/build/uwsgi/build/uwsgi/plugins/python/python_plugin.o
/srv/www/li/venv/build/uwsgi/plugins/python/python_plugin.o
sudo find / -name 'python_plugin.so'
Run Code Online (Sandbox Code Playgroud)

但没有发现.我之前的系统通过apt-get进行了uwsgi安装,但这确实很旧(而且我很确定它通常使用pip安装的uwsgi,但是对于共享对象可能不行吗?)

一些背景信息:

  • Ubuntu 12.0.4 LTS
  • Python 2.7(virtualenv)
  • 我已经在我的venv中安装了uWSGI,正常使用pip install uwsgi(没有sudo)

所以我有点无能为力:(我不能成为世界上唯一拥有此权利的人,对吧?我应该自己编译.so对象吗?(如果是这样,怎么样?)或者还有另一个很好的解决方案吗?

python uwsgi

30
推荐指数
3
解决办法
3万
查看次数

如何将独立的Python脚本集成到Rails应用程序中?

我有一个程序,它有一个小的文件结构,然后运行使用

python do_work.py foo bar
Run Code Online (Sandbox Code Playgroud)

我希望我的Rails用户按下按钮并为他们发生这种情况,结果要么上传到某个地方,要么只是作为下载链接或类似的东西抛出 - 输出do_work.py(比方说,它result.txt)

我还想澄清一下,脚本会在文件系统上创建3个独立的文件,这些文件不是文本文件(这应该不重要,这里不是问题)

最好的方法是什么?可以rake运行exec Python吗?更重要的是,这在heroku上是可行的吗?

我在我的系统上安装了Python,但sockmonk提供的答案似乎不起作用 - 它返回nil.请注意,其他命令ls似乎有效.

这可能是权限问题吗?

def index
    value = %x( python --version )
    render :text => value
end
Run Code Online (Sandbox Code Playgroud)

顺便说一句,尝试这个irb:

%x(python)
Run Code Online (Sandbox Code Playgroud)

打开irb的Python终端INSIDE.然而,无论出于何种原因,它都不会占用.

ruby python rake ruby-on-rails heroku

30
推荐指数
1
解决办法
9902
查看次数

'return'和'pure'之间的区别

之间有什么区别returnpure距离Control.Applicative?似乎我pure甚至可以在一个do块结束时使用它?

那么在任何情况下,一个人应该优先于另一个(除了每个人都希望return在一个do区块的末尾)?

haskell

28
推荐指数
2
解决办法
3255
查看次数