I am trying to force myself to understand how to use classes. My code has gotten longer and messier. I am hoping that using classes will help me clean it up some
from lxml import html
Class Header(object):
def __init__(self,file_reference)
self.header = open(file_reference).read()
def filing_type(self):
tree = html.fromstring(self)
for element in tree.iter():
if element.tag == 'type':
return element.text.strip()
so I have a reference to a particular file
myref = 'correct_file_path'
test_header = Header(myref)
when I do a dir(test_header) I see my filing_type function in the list. However when I run
test_header.filing_type()
I get a TypeError
TypeError: 'Header' object is not subscriptable
You should use tree = html.fromstring(self.header)
. And it seems your return
in filling_type
is wrong because it will only return the first tag's contents(Maybe you just want this). Maybe you could use a list to store all the type tags or use yield.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.