我正在列表中执行for循环,并希望找到匹配并打印下一行。但是,当我尝试使用next()方法时,我一直都会失败。我可以在指定的匹配后获取下一行的帮助吗?
string(用于循环输出):
item_0
0
item_1
0
item_3
727
item_4
325
For Loop查找匹配和下一行:
result = tree.xpath('//tr/td/font/text()')
for line in result:
if 'item_3' in line:
print(line.next())
错误:
AttributeError: '_ElementStringResult' object has no attribute 'next'
答案 0 :(得分:2)
line
是代码中的lxml.etree._ElementStringResult
(修改后的str
)。 lxml.etree._ElementStringResults
没有next
方法,这就是您获得AttributeError
的原因。
您可以设置一个标志,指示下一行应按如下方式打印:
print_line = False
for line in result:
if print_line:
print(line)
print_line = False
if 'item_3' in line:
print_line = True
答案 1 :(得分:1)
没有测试过,但请尝试:
//virtual base class
class Base {
public :
virtual void f() {
cout << "Base::f()" << endl;
}
private:
long x;
};
//derived class
class Derived : public virtual Base {
public:
virtual void f() {
cout << "Derived::f()" << endl;
}
private:
long y;
};
int main() {
typedef void (*FUNC)(void);
Derived d;
//In my machine, sizeof(long) == sizeof(pointers). My code below is neither portable nor concise. You can just read the annotation.
//dereference the first element of the first virtual function table(equals to *(vptr1->slot[0]))
cout << hex << *((long*)*((long*)(&d) + 0) + 0) << endl;
((FUNC)*((long*)*((long*)(&d) + 0) + 0))();//invoke Derived::f()
//dereference the first element of the second virtual function table(equals to *(vptr2->slot[0]))
cout << hex << *((long*)*((long*)(&d) + 2) + 0) << endl;
((FUNC)*((long*)*((long*)(&d) + 2) + 0))();//maybe Derived::f()?
return 0;
}