I tried to save tables from a document as a file under a directory as follows:
for table in tables:
tableString = html.tostring(table)
fileref=open('c:\\Users\\ahn_133\\Desktop\\appleTables\\Apple-' + str(count) + '.htm', 'w')
fileref.write(tableString)
fileref.close()
count+=1
But, I keep getting an error as follows:
Traceback (most recent call last):
File "<pyshell#27>", line 4, in <module>
fileref.write(tableString)
TypeError: must be str, not bytes
I am using Python 3.3 and installed lxml-3.0.1.win32-py3.3.exe
How can I fix this error?
lxml’s
tostringmethod returns a bytestring (bytes), because it is already encoded. This is necessary because the XML/HTML document can specify its own encoding, and that better be right!Simply open the file in binary mode: