stringio

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

Real file objects slower than StringIO and cStringIO?

别等时光非礼了梦想. 提交于 2019-12-05 07:29:01
问题 StringIO has the following notes in its code: Notes: - Using a real file is often faster (but less convenient). - There's also a much faster implementation in C, called cStringIO, but it's not subclassable. The "real file is often faster" line seemed really odd to me: how could writing to disk beat writing to memory? I tried profiling these different cases and got results that contradict these docs, as well as the answer to this question. This other question does explain why cStringIO is

Why is StringIO object slower than real file object?

你。 提交于 2019-12-05 06:57:17
I'm looking through the source of StringIO where it says says some notes: Using a real file is often faster (but less convenient) . There's also a much faster implementation in C, called cStringIO , but it's not subclassable. StringIO just like a memory file object, why is it slower than real file object? Python's file handling is implemented entirely in C . This means that it's quite fast (at least in the same order of magnitude as native C code). The StringIO library, however, is written in Python. The module itself is thus interpreted, with the associated performance penalties. As you know,

Storing image using open URI and paperclip having size less than 10kb

巧了我就是萌 提交于 2019-12-05 00:49:08
问题 I want to import some icons from my old site. The size of those icons is less than 10kb. So when I am trying to import the icons its returning stringio.txt file. require "open-uri" class Category < ActiveRecord::Base has_attached_file :icon, :path => ":rails_root/public/:attachment/:id/:style/:basename.:extension" def icon_from_url(url) self.icon = open(url) end end In rake task. category = Category.new category.icon_from_url "https://xyz.com/images/dog.png" category.save 回答1: Try: def icon

pandas unable to read from large StringIO object

孤街醉人 提交于 2019-12-04 19:05:56
问题 I'm using pandas to manage a large array of 8-byte integers. These integers are included as space-delimited elements of a column in a comma-delimited CSV file, and the array size is about 10000x10000. Pandas is able to quickly read the comma-delimited data from the first few columns as a DataFrame, and also quickly store the space-delimited strings in another DataFrame with minimal hassle. The trouble comes when I try to cast transform the table from a single column of space-delimited strings

Python's StringIO doesn't do well with `with` statements

五迷三道 提交于 2019-12-04 10:04:33
问题 I need to stub tempfile and StringIO seemed perfect. Only that all this fails in an omission: In [1]: from StringIO import StringIO In [2]: with StringIO("foo") as f: f.read() --> AttributeError: StringIO instance has no attribute '__exit__' What's the usual way to provide canned info instead of reading files with nondeterministic content? 回答1: The StringIO module predates the with statement. Since StringIO has been removed in Python 3 anyways, you can just use its replacement, io.BytesIO: >>

Python pandas NameError: StringIO is not defined

自古美人都是妖i 提交于 2019-12-04 05:15:33
I am unable to read data in Pandas: Input: import pandas as pd data = 'a,b,c\n1,2,3\n4,5,6' pd.read_csv(StringIO(data),skipinitialspace=True) Output: NameError:name 'StringIO' is not defined Please let me know why the error occurred and also let me know what to import. Abhishek Found the solution here: The error occurred because I didn't import StringIO . Unlike Python 2, in Python 3 you are required to import it. from io import StringIO After importing no error occurred. Output to the above question was: a b c 0 1 2 3 1 4 5 6 It can also be imported from pandas.compat which works for both

Serving Excel(xlsx) file to the user for download in Django(Python)

为君一笑 提交于 2019-12-04 04:54:20
I'm trying create and serve excel files using Django. I have a jar file which gets parameters and produces an excel file according to parameters and it works with no problem. But when i'm trying to get the produced file and serve it to the user for download the file comes out broken. It has 0kb size. This is the code piece I'm using for excel generation and serving. def generateExcel(request,id): if os.path.exists('./%s_Report.xlsx' % id): excel = open("%s_Report.xlsx" % id, "r") output = StringIO.StringIO(excel.read()) out_content = output.getvalue() output.close() response = HttpResponse(out

Real file objects slower than StringIO and cStringIO?

点点圈 提交于 2019-12-03 22:17:15
StringIO has the following notes in its code: Notes: - Using a real file is often faster (but less convenient). - There's also a much faster implementation in C, called cStringIO, but it's not subclassable. The "real file is often faster" line seemed really odd to me: how could writing to disk beat writing to memory? I tried profiling these different cases and got results that contradict these docs, as well as the answer to this question . This other question does explain why cStringIO is slower under some circumstances, though I'm not doing any concatenating here. The test writes a given

pandas unable to read from large StringIO object

这一生的挚爱 提交于 2019-12-03 12:31:37
I'm using pandas to manage a large array of 8-byte integers. These integers are included as space-delimited elements of a column in a comma-delimited CSV file, and the array size is about 10000x10000. Pandas is able to quickly read the comma-delimited data from the first few columns as a DataFrame, and also quickly store the space-delimited strings in another DataFrame with minimal hassle. The trouble comes when I try to cast transform the table from a single column of space-delimited strings to a DataFrame of 8-bit integers. I have tried the following: intdata = pd.DataFrame(strdata