stringio

python PIL image how to save image to a buffer so can be used later?

China☆狼群 提交于 2020-01-21 07:49:05
问题 I have a png file which should be convert to jpg and save to gridfs , I use python's PIL lib to load the file and do the converting job, the problem is I want to store the converted image to a MongoDB Gridfs, in the saving procedure, I can't just use the im.save() method. so I use a StringIO to hold the temp file but it don't work. here is the code snippet: #!/usr/bin/env python # -*- coding: utf-8 -*- from PIL import Image from pymongo import MongoClient import gridfs from StringIO import

How to use string as input for csv reader without storing it to file

故事扮演 提交于 2020-01-14 10:28:09
问题 I'm trying to loop through rows in a csv file. I get csv file as string from a web location. I know how to create csv.reader using with when data is stored in a file. What I don't know is, how to get rows using csv.reader without storing string to a file. I'm using Python 2.7.12. I've tried to create StringIO object like this: from StringIO import StringIO csv_data = "some_string\nfor_example" with StringIO(csv_data) as input_file: csv_reader = reader(csv_data, delimiter=",", quotechar='"')

How to use string as input for csv reader without storing it to file

落花浮王杯 提交于 2020-01-14 10:28:05
问题 I'm trying to loop through rows in a csv file. I get csv file as string from a web location. I know how to create csv.reader using with when data is stored in a file. What I don't know is, how to get rows using csv.reader without storing string to a file. I'm using Python 2.7.12. I've tried to create StringIO object like this: from StringIO import StringIO csv_data = "some_string\nfor_example" with StringIO(csv_data) as input_file: csv_reader = reader(csv_data, delimiter=",", quotechar='"')

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

StringIO example does not work

孤街醉人 提交于 2020-01-11 09:36:10
问题 I try to understand how works numpy.getfromtxt method and io.StringIO . On the officical website(https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.genfromtxt.html#numpy.genfromtxt) I found some examples. Here is one of them: s = StringIO("1,1.3,abcde") data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'),('mystring','S5')], delimiter=",") But when I run this code on my computer I get: TypeError: must be str or None, not bytes Tell me please how to fix it? 回答1: In

Python(2.6) cStringIO unicode support?

谁说胖子不能爱 提交于 2020-01-02 06:33:13
问题 I'm using python pycurl module to download content from various web pages. Since I also wanted to support potential unicode text I've been avoiding the cStringIO.StringIO function which according to python docs: cStringIO - Faster version of StringIO Unlike the StringIO module, this module is not able to accept Unicode strings that cannot be encoded as plain ASCII strings. ... does not support unicode strings. Actually it states that it does not support unicode strings that can not be

PDF does not contain EOF marker (PDF::Reader::MalformedPDFError) with pdf-reader

拥有回忆 提交于 2020-01-01 10:50:08
问题 I am using ‘pdf-reader’ gem to read raw contents of pdf documents so I can post (http-post) them to an API. To confirm the API implementation can create a valid pdf document from the raw content, I wrote a small gist to validate my code to read the pdf content. For some reason, I always keep getting ‘PDF does not contain EOF marker (PDF::Reader::MalformedPDFError)’. Can anyone point me out where I am going wrong? 来源: https://stackoverflow.com/questions/27612426/pdf-does-not-contain-eof-marker

Python Capture reply from powershell

限于喜欢 提交于 2019-12-31 05:15:12
问题 The code below works when typed manually however when I run the program.py nothing prints. My ultimate goal is to retrieve this data from user pc to create an easy way to recreate shortcuts.... My users somehow lose them lol import smtplib, os, subprocess, sys from string import ascii_uppercase from cStringIO import StringIO data = os.popen(r"dir %userprofile%\desktop\*.lnk* /s/b").read() file = open("testitem.txt", "w") file.write(data) file.close() my_data = dict(zip(ascii_uppercase,open(

StringIO replacement that works with bytes instead of strings?

 ̄綄美尐妖づ 提交于 2019-12-28 11:41:14
问题 Is there any replacement for python StringIO class, one that will work with bytes instead of strings? It may not be obvious but if you used StringIO for processing binary data you are out of luck with Python 2.7 or newer. 回答1: Try io.BytesIO. As others have pointed out, you can indeed use StringIO in 2.7, but BytesIO is a good choice for forward-compatibility. 回答2: In Python 2.6/2.7, the io module is intended to be used for compatibility with Python 3.X. From the docs: New in version 2.6. The

StringIO portability between python2 and python3 when capturing stdout

旧巷老猫 提交于 2019-12-24 00:57:51
问题 I have written a python package which I have managed to make fully compatible with both python 2.7 and python 3.4, with one exception that is stumping me so far. The package includes a command line script, and in my unit tests I use this code to run the script's main routine while overriding sys.argv to pass command line arguments for argparse, and capturing the script's stdout for comparison: @contextlib.contextmanager def runmain(mainfunction, arglist): """Run mainfunction with arglist in