bytesio

Why is TextIOWrapper closing the given BytesIO stream?

谁说我不能喝 提交于 2021-01-27 19:40:21
问题 If I run following code in python 3 from io import BytesIO import csv from io import TextIOWrapper def fill_into_stringio(input_io): writer = csv.DictWriter(TextIOWrapper(input_io, encoding='utf-8'),fieldnames=['ids']) for i in range(100): writer.writerow({'ids': str(i)}) with BytesIO() as input_i: fill_into_stringio(input_i) input_i.seek(0) I get an error: ValueError: I/O operation on closed file. While if I don't use the TextIOWrapper the io stream stays open. As an example if I modify my

How to Save io.BytesIO pdfrw PDF into Django FileField

旧时模样 提交于 2021-01-27 13:39:17
问题 What I am trying to do is basically: Get PDF from URL Modify it via pdfrw Store it in memory as a BytesIO obj Upload it into a Django FileField via Model.objects.create(form=pdf_file, name="Some name") My issue is that when the create() method runs, it saves all of the fields except for the form . helpers.py import io import tempfile from contextlib import contextmanager import requests import pdfrw @contextmanager def as_file(url): with tempfile.NamedTemporaryFile(suffix='.pdf') as tfile:

Error decode byte when send image in discord

拜拜、爱过 提交于 2020-04-17 21:41:26
问题 I have some problems with sending images in discord. I decide to use Pillow library for creating images and I want to send image which is created by this library without save . I found out what I can convert Image object to binary data and put in fp argument. But it raised encoding error. Code: image = Image.open("test.png") image_binary = BytesIO() image.save(image_binary, "PNG") image_binary = image_binary.getvalue() await ctx.send(file=discord.File(fp=image_binary)) Error: Traceback (most

Can I pipe a io.BytesIO() stream to subprocess.popen() in Python?

删除回忆录丶 提交于 2020-03-14 06:58:16
问题 I'm trying to pipe a io.BytesIO() bytetream to a separate program using subprocess.popen() , but I don't know how or if this is at all possible. Documentation and examples are all about text and newlines. When I whip up something like this: import io from subprocess import * stream = io.BytesIO() someStreamCreatingProcess(stream) command = ['somecommand', 'some', 'arguments'] process = Popen(command, stdin=PIPE) process.communicate(input=stream) I get Traceback (most recent call last): File "

Confusing about StringIO, cStringIO and ByteIO

一世执手 提交于 2020-01-12 06:31:26
问题 I have googled and also search on SO for the difference between these buffer modules. However, I still don't understand very well and I think some of the posts I read are out of date. In Python 2.7.11, I downloaded a binary file of a specific format using r = requests.get(url) . Then I passed StringIO.StringIO(r.content) , cStringIO.StringIO(r.content) and io.BytesIO(r.content) to a function designed for parsing the content. All these three methods are available. I mean, even if the file is

How to convert Pandas DataFrame to bytes-like object

隐身守侯 提交于 2020-01-02 04:59:05
问题 Hi I am trying to convert my df to binary and store it in a variable. my_df: df=pd.DataFrame({'A':[1,2,3],'B':[4,5,6]}) my code: import io towrite = io.BytesIO() df.to_excel(towrite) # write to BytesIO buffer towrite.seek(0) # reset pointer I am getting AttributeError: '_io.BytesIO' object has no attribute 'write_cells' Full Traceback: AttributeError Traceback (most recent call last) <ipython-input-25-be6ee9d9ede6> in <module>() 1 towrite = io.BytesIO() ----> 2 df.to_excel(towrite) # write to

Why does truncating a BytesIO mess it up?

血红的双手。 提交于 2019-12-24 07:45:01
问题 Running this on Python 3.5.1 on OSX: import io b = io.BytesIO() b.write(b'222') print(b.getvalue()) b.truncate(0) b.write(b'222') print(b.getvalue()) Produces: b'222' b'\x00\x00\x00222' So truncating the BytesIO somehow causes it to start inserting extra zero bytes in the beginning? Why? 回答1: truncate does not move the file pointer. So the next byte is written to the next position. You have also to seek to the beginning: b.seek(0) b.truncate() 来源: https://stackoverflow.com/questions/39109068