Linux的
>>> from lxml import etree
>>> html='''<td><a href=''>a1</a></td>
... <td><a href=''>a2</a></td>
... '''
>>> p=etree.HTML(html)
>>> a=p.xpath("//a[1]")
>>> for i in a:
... print i.text
...
a1
a2
Run Code Online (Sandbox Code Playgroud)
视窗.
>>> html='''<td><a href=''>a1</a></td>
... <td><a href=''>a2</a></td>
... '''
>>> from lxml import etree
>>> p=etree.HTML(html)
>>> a=p.xpath("//a[1]")
>>> for i in a:
... print i.text
...
a1
>>> b=p.xpath("//a[2]")
>>> for i in b:
... print i.text
...
a2
Run Code Online (Sandbox Code Playgroud)
在Windows中,我可以轻松地使用a[1]并a[2]获得这两个值.但是在Linux中,xpath //a[1]将这两个链接文本放在一起.
这使得程序在这些操作系统中不那么兼容.我必须修改不同操作系统上的代码.它是一个lxml模块错误吗?对此有何解决方案?
我有一个数据表如下:
A B C
type1 A1 B1 C1
type2 A2 B2 C2
Run Code Online (Sandbox Code Playgroud)
我使用下面的代码
d={}
D={}
h = ['A','B','C']
type1=['A1','B1','C1']
type2=['A2','B2','C2']
for i,val in enumerate(h):
d['Type1'] = type1[i]
d['Type2'] = type2[i]
D[val]=d
print('loop',i,'\nd:',d,'\nD:',D,'\n\n====')
#print(D)
Run Code Online (Sandbox Code Playgroud)
我希望得到以下dict作为结果:
{'A':{'Type1':'A1','Type2':'A2'},'B':{'Type1':'B1','Type2':B2},'C':{'Type1':C1,'Type2':'C2'},}
Run Code Online (Sandbox Code Playgroud)
但是,输出是:
{'A': {'Type1': 'C1', 'Type2': 'C2'}, 'B': {'Type1': 'C1', 'Type2': 'C2'}, 'C': {'Type1': 'C1', 'Type2': 'C2'}}
Run Code Online (Sandbox Code Playgroud)
我逻辑中的错误是什么?
我无法弄清楚那里有什么问题.
我在循环中添加了一个打印件.
loop 0
d: {'Type1': 'A1', 'Type2': 'A2'}
D: {'A': {'Type1': 'A1', 'Type2': 'A2'}}
====
loop 1
d: {'Type1': 'B1', 'Type2': 'B2'}
D: {'A': {'Type1': 'B1', 'Type2': …Run Code Online (Sandbox Code Playgroud) 有一个像这样的数据框:
df = pd.DataFrame((['1990-01-01','A','S1','2','string2','string3'],
['1990-01-01','A','S2','1','string1','string4'],
['1990-01-01','A','S3','1','string5','string6']),columns=
["date","type","status","count","s1","s2")
date type status count s1 s2
0 1990-01-01 A S1 2 string2 string3
1 1990-01-01 A S2 1 string1 string4
2 1990-01-01 A S3 1 string5 string6
...
Run Code Online (Sandbox Code Playgroud)
我想要得到以下结果(每个日期和每种类型应该有单行,并获取 s1 列的最小值,获取 s2 列的最大值)
date type S1 S2 S3 min_s1 max_s2
1990-01-01 A 2 1 1 string1 string6
Run Code Online (Sandbox Code Playgroud)
我尝试使用pivot_table
df.pivot_table(index=['date','type'],columns=['status'],values=['count','s1','s2'], aggfunc={
'count':np.sum,
's1': np.min,
's2': np.max
})
Run Code Online (Sandbox Code Playgroud)
但这只会得到以下结果,这会导致多列而不是最终结果。
count s1 s2
status S1 S2 S3 S1 S2 S3 S1 S2 S3
date type …Run Code Online (Sandbox Code Playgroud) 在烧瓶中,
@app.route('/test')
def test():
test = ['a','b','c','d','e','f','g',['1','2','3']]
return render_template('test.html',data=test)
Run Code Online (Sandbox Code Playgroud)
这是我的模板:
{% for item in data %}
<tr>
<td>{{item.0}}</td>
<td>{{item.1}}</td>
<td>{{item.2}}</td>
<td>{{item.3}}</td>
<td>{{item.4}}</td>
<td>{{item.5}}</td>
<td>{{item.6}}</td>
</tr>
<tr>
<td colspan="5">{{ item[7][0]}}</td>
<td>{{ item[7][1]}}</td>
<td>{{ item[7][2]}}</td>
</tr>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
但是总是报错:
jinja2.exceptions.UndefinedError: str object has no element 7
Run Code Online (Sandbox Code Playgroud)
如何获取列表中的列表?
看起来像jinja2需要字符串,但它不能 regonize pythonitem[7][0]格式。我也试过item.7.0and item.7[0],但同样的错误。
谢谢!