bytesio

Reading in pydub AudioSegment from url. BytesIO returning “OSError [Errno 2] No such file or directory” on heroku only; fine on localhost

大憨熊 提交于 2019-12-06 15:55:22
EDIT 1 for anyone with the same error: installing ffmpeg did indeed solve that BytesIO error EDIT 1 for anyone still willing to help: my problem is now that when I AudioSegment.export("filename.mp3", format="mp3"), the file is made, but has size 0 bytes -- details below (as "EDIT 1") EDIT 2: All problems now solved. Files can be read in as AudioSegment using BytesIO I found buildpacks to ensure ffmpeg was installed correctly on my app, with lame support for exporting proper mp3 files Answer below Original question I have pydub working nicely locally to crop a particular mp3 file based on

Strange “BadZipfile: Bad CRC-32” problem

谁都会走 提交于 2019-12-05 09:48:57
This code is simplification of code in a Django app that receives an uploaded zip file via HTTP multi-part POST and does read-only processing of the data inside: #!/usr/bin/env python import csv, sys, StringIO, traceback, zipfile try: import io except ImportError: sys.stderr.write('Could not import the `io` module.\n') def get_zip_file(filename, method): if method == 'direct': return zipfile.ZipFile(filename) elif method == 'StringIO': data = file(filename).read() return zipfile.ZipFile(StringIO.StringIO(data)) elif method == 'BytesIO': data = file(filename).read() return zipfile.ZipFile(io

In an HTML table, how to add text beside plot in jupyter notebook using python?

对着背影说爱祢 提交于 2019-12-02 08:18:34
Any ideas on how to create a 1 X 2 HTML table where cell {0} is a matplotlib plot and cell {1} is a text description for Python 3.X? import matplotlib.pyplot as plt from io import BytesIO %matplotlib inline def add_split_screen(fig, text, iwidth=None): figdata = BytesIO() fig.savefig(figdata, format='png') figdata.seek(0) figdata figdata.close() iwidth = ' width={0} '.format(iwidth) if iwidth is not None else '' datatable = '<table><tr><td>{0}</td><td>{1}</td></tr></table>'.format(figdata, text) display(HTML(datatable)) Setting up a test case: fig, ax = plt.subplots(1,1, figsize=(6,4)) ax.plot

Writing to io.BytesIO in csv fails in python3

我们两清 提交于 2019-11-29 13:12:17
I am trying to write python 2/3 compatible code to write strings to csv file object. This code: line_as_list = [line.encode() for line in line_as_list] writer_file = io.BytesIO() writer = csv.writer(writer_file, dialect=dialect, delimiter=self.delimiter) for line in line_as_list: assert isinstance(line,bytes) writer.writerow(line) Gives this error on Python3: > writer.writerow(line) E TypeError: a bytes-like object is required, not 'str' But assert has no problem with the type, so why is csv creating an error? Can't I use BytesIO only for both Python 2 and 3? Where is the problem here? In

Writing then reading in-memory bytes (BytesIO) gives a blank result

跟風遠走 提交于 2019-11-28 20:02:30
I wanted to try out the python BytesIO class. As an experiment I tried writing to a zip file in memory, and then reading the bytes back out of that zip file. So instead of passing in a file-object to gzip , I pass in a BytesIO object. Here is the entire script: from io import BytesIO import gzip # write bytes to zip file in memory myio = BytesIO() g = gzip.GzipFile(fileobj=myio, mode='wb') g.write(b"does it work") g.close() # read bytes from zip file in memory g = gzip.GzipFile(fileobj=myio, mode='rb') result = g.read() g.close() print(result) But it is returning an empty bytes object for

Writing to io.BytesIO in csv fails in python3

妖精的绣舞 提交于 2019-11-28 02:44:31
问题 I am trying to write python 2/3 compatible code to write strings to csv file object. This code: line_as_list = [line.encode() for line in line_as_list] writer_file = io.BytesIO() writer = csv.writer(writer_file, dialect=dialect, delimiter=self.delimiter) for line in line_as_list: assert isinstance(line,bytes) writer.writerow(line) Gives this error on Python3: > writer.writerow(line) E TypeError: a bytes-like object is required, not 'str' But assert has no problem with the type, so why is csv

Writing then reading in-memory bytes (BytesIO) gives a blank result

冷暖自知 提交于 2019-11-27 12:54:02
问题 I wanted to try out the python BytesIO class. As an experiment I tried writing to a zip file in memory, and then reading the bytes back out of that zip file. So instead of passing in a file-object to gzip , I pass in a BytesIO object. Here is the entire script: from io import BytesIO import gzip # write bytes to zip file in memory myio = BytesIO() g = gzip.GzipFile(fileobj=myio, mode='wb') g.write(b"does it work") g.close() # read bytes from zip file in memory g = gzip.GzipFile(fileobj=myio,

PIL cannot identify image file for io.BytesIO object

[亡魂溺海] 提交于 2019-11-27 01:59:13
I am using the Pillow fork of PIL and keep receiving the error OSError: cannot identify image file <_io.BytesIO object at 0x103a47468> when trying to open an image. I am using virtualenv with python 3.4 and no installation of PIL. I have tried to find a solution to this based on others encountering the same problem, however, those solutions did not work for me. Here is my code: from PIL import Image import io # This portion is part of my test code byteImg = Image.open("some/location/to/a/file/in/my/directories.png").tobytes() # Non test code dataBytesIO = io.BytesIO(byteImg) Image.open