Page 1 of 1
remove whitespace while reading file
Posted: Tue Jun 12, 2018 8:03 am
by devnull
Code: Select all
self.chunk = 2048
with open(self.fdir+req['path'], 'rb') as html:
head = self.head(req['path'])
chunk = head + html.read(self.chunk - len(head))
while chunk:
res.sendall(chunk)
chunk = html.read(self.chunk)
res.close()
It tried strip() but that does not work, how can I compress the read (html) file by removing whitespaces and carriage returns before sending ?
Re: remove whitespace while reading file
Posted: Tue Jun 12, 2018 8:24 am
by Roberthh
e.g.
"".join(chunk.split())
Re: remove whitespace while reading file
Posted: Tue Jun 12, 2018 9:51 am
by devnull
Thanks, that results in:
Code: Select all
Error: join expects a list of str/bytes objects consistent with self object
Also tried:
Code: Select all
"".join(chunk.decode().split()).encode()
By that's no good either !
Re: remove whitespace while reading file
Posted: Tue Jun 12, 2018 11:09 am
by Roberthh
if chunk is a bytes object, you may need:
b"".join(chunk.split())
Re: remove whitespace while reading file
Posted: Wed Jun 13, 2018 7:10 am
by pythoncoder
The unicode support in Python 3 requires constant attention to detail regarding the difference between bytes (and bytearray) objects and strings. It still regularly catches me out...