Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
4 ตอบกลับ
18610 มุมมอง

Odoo 11, uses Python3, which is good, but I wonder what is the best way to read/write to file-like objects.

Code which seemed to work like a charm in Py2.7, failed to execute in Py3, thus I wonder what could I be doing wrong...

I did changed import StringIO from io, but no luck. Anybody faced issues like this, any suggestions would be appreciated.

from io import StringIO

wb = xlwt.Workbook(encoding="UTF-8")
ws = wb.add_sheet('Sheet1'
....

fp = StringIO()
wb.save(fp)
fp.seek(0)
data = fp.read()
fp.close()


Error:

File "/usr/lib/python3.6/zipfile.py", line 1784, in _write_end_record
self.fp.write(endrec)
TypeError: string argument expected, got 'bytes'

Thanks

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi,

Instead of StringIO try with BytesIO

check this for more info : Error. string argument expected, got 'bytes'

Thanks

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

In python3 The StringIO and cStringIO modules are gone. You can import io and use io.StringIO or io.BytesIO

eg:

import io
f = io.BytesIO(b"some initial binary data: \x00\x01")
f = io.StringIO("some initial text data")



อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hey Deep,

Please Try with ,

from io import BytesIO

fp = BytesIO()

For more ref :

> \https://stackoverflow.com/questions/37462075/confusing-about-stringio-cstringio-and-byteio

Thanks.

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
2
ก.พ. 24
15419
1
ธ.ค. 22
4930
2
ธ.ค. 22
14350
2
มิ.ย. 22
6188
2
มิ.ย. 22
4598