Tag: python

如何删除lxml中的元素

我需要使用python的lxml完全删除基于属性内容的元素。 例: import lxml.etree as et xml=""" <groceries> <fruit state="rotten">apple</fruit> <fruit state="fresh">pear</fruit> <fruit state="fresh">starfruit</fruit> <fruit state="rotten">mango</fruit> <fruit state="fresh">peach</fruit> </groceries> """ tree=et.fromstring(xml) for bad in tree.xpath("//fruit[@state=\'rotten\']"): #remove this element from the tree print et.tostring(tree, pretty_print=True) 我想这打印: <groceries> <fruit state="fresh">pear</fruit> <fruit state="fresh">starfruit</fruit> <fruit state="fresh">peach</fruit> </groceries> 有没有办法做到这一点,而不是存储一个临时variables,并手动打印,如: newxml="<groceries>\n" for elt in tree.xpath('//fruit[@state=\'fresh\']'): newxml+=et.tostring(elt) newxml+="</groceries>"

使用matplotlib面向对象的界面与seaborn绘图

我非常喜欢在OOP风格中使用matplotlib : f, axarr = plt.subplots(2, sharex=True) axarr[0].plot(…) axarr[1].plot(…) 这使得更容易跟踪多个数字和子图。 问:如何使用seaborn这种方式? 或者,如何将此示例更改为OOP样式? 如何告诉seaborn绘图function, Figure或Figure lmplot ?

将dynamicPython对象转换为JSON

可能重复: Python可序列化的对象json 我需要知道如何将一个dynamic的python对象转换成JSON。 该对象必须能够具有多个级别对象的子对象。 例如: class C(): pass class D(): pass c = C() c.dynProperty1 = "something" c.dynProperty2 = { 1, 3, 5, 7, 9 } cd = D() cddynProperty3 = "d.something" # … convert c to json … 使用python 2.6下面的代码: import json class C(): pass class D(): pass c = C() c.what = "now?" […]

过滤python字典中包含特定string的项目

我是一个用python开发东西的C编码器。 我知道如何在C中进行以下操作(因此,在Python中使用类似于C的逻辑),但是我想知道“Python”的做法是什么。 我有一个字典d,我想操作的项目的一个子集,只有谁的关键(string)包含一个特定的子string。 即C逻辑将是: for key in d: if filter_string in key: # do something else # do nothing, continue 我想像的python版本会是这样的 filtered_dict = crazy_python_syntax(d, substring) for key,value in filtered_dict.iteritems(): # do something 我在这里发现了很多关于过滤词典的post,但是找不到一个涉及到这个的post。 我的字典不是嵌套的,我使用Python 2.7

Python打印语句“语法错误:无效语法”

为什么Python在第9行的简单print语句中给我一个语法错误? import hashlib, sys m = hashlib.md5() hash = "" hash_file = raw_input("What is the file name in which the hash resides? ") wordlist = raw_input("What is your wordlist? (Enter the file name) ") try: hashdocument = open(hash_file,"r") except IOError: print "Invalid file." # Syntax error: invalid syntax raw_input() sys.exit() else: hash = hashdocument.readline() hash […]

懒惰的logging器消息string评估

我在我的Python应用程序中使用标准的Python日志logging模块: 导入日志 logging.basicConfig(级别= logging.INFO) logger = logging.getLogger(“log”) 而真: logger.debug('stupid log message'+''.join([str(i)for i in range(20)])) # 做一点事 问题是虽然debugging级别没有启用,但是愚蠢的日志消息在每次循环迭代中被评估,这严重损害了性能。 有没有解决scheme? 在C ++中,我们有提供这样的macros的log4cxx包: LOG4CXX_DEBUG(logger, messasage) 这有效评估 if(log4cxx :: debugEnabled(logger)){ log4cxx.log(logger,log4cxx :: LOG4CXX_DEBUG,message) } 但是由于Python中没有macros(AFAIK),如果有一个有效的方法来做日志?

python从DataFrame制作热图

我有一个从Python的pandas包生成的数据框。 如何使用pandas包中的DataFrame生成热图。 import numpy as np from pandas import * Index= ['aaa','bbb','ccc','ddd','eee'] Cols = ['A', 'B', 'C','D'] df = DataFrame(abs(np.random.randn(5, 4)), index= Index, columns=Cols) >>> df ABCD aaa 2.431645 1.248688 0.267648 0.613826 bbb 0.809296 1.671020 1.564420 0.347662 ccc 1.501939 1.126518 0.702019 1.596048 ddd 0.137160 0.147368 1.504663 0.202822 eee 0.134540 3.708104 0.309097 1.641090 >>>

如何在Jinja2中sorting列表?

我正在尝试这样做: {% for movie in movie_list | sort(movie.rating) %} 但是这是不正确的…文件是模糊的…你怎么在Jinja2做这个?

如何使Heroku上的Python只有https?

我有Heroku(雪松堆栈)上的python / django应用程序,并希望通过https访问它。 我已经启用了“ssl piggyback”选项,并可以通过https连接到它。 但是,禁用http访问或redirect到https的最佳方法是什么?

在Python 2.7中将zip文件内容解压缩到特定的目录

这是我目前使用的代码提取一个zip文件,该文件与脚本位于当前工作目录中。 我怎样才能指定一个不同的目录来提取? 我试过的代码不是在我想要的地方提取。 import zipfile fh = open('test.zip', 'rb') z = zipfile.ZipFile(fh) for name in z.namelist(): outfile = open(name, 'wb') outfile.write('C:\\'+z.read(name)) outfile.close() fh.close()