stringio

get index per ID – from example data using text input to pandas dataframe? easily reproducible

get index per ID – from example data using text input to pandas dataframe? easily reproducible Question: Text can be used as input to pandas dataframes to make easily reproducible examples for testing solutions.1 import pandas as pd from io import StringIO txt= """ ID,datetime,value AB-CL-34,07/10/2022 10:00:00,5 AB-CL-34,07/10/2022 11:15:10,7 AB-CL-34,09/10/2022 15:30:30,13 BX-RT-55,06/10/2022 11:30:22,0 BX-RT-55,10/10/2022 22:44:11,1 …

Total answers: 1

How to check what lineending a StringIO file is using?

How to check what lineending a StringIO file is using? Question: I had a method that detects line endings def getLineEnding(filename): ret = "rn" with open(filename, ‘r’) as f: f.readline() ret = f.newlines return ret In order to be able to test it without using real files, I changed it to: def getLineEnding(filehandle): filehandle.readline() return …

Total answers: 1

pysftp putfo creates an empty file on SFTP server but not streaming the content from StringIO

pysftp putfo creates an empty file on SFTP server but not streaming the content from StringIO Question: My code first writes lines to a CSV in io.StringIO(): fileBuffer = io.StringIO() # write header header_writer = csv.DictWriter(fileBuffer, fieldnames=columnNames) header_writer.writeheader() # write lines writer = csv.writer(fileBuffer, delimiter=’,’) for line in data: line_dec = line.decode(‘ISO-8859-1’) # print([line_dec]) writer.writerow([line_dec]) …

Total answers: 1

python3 print to string

python3 print to string Question: Using Python 3, I have a console application that I am porting to a GUI. The code has a bunch of print statements, something like this: print(f1(), f2(), f3(), sep=getsep(), end=getend()) I would like to convert these calls into something like: GuiPrintLine(f1(), f2(), f3(), sep=getsep(), end=getend()) where each line is …

Total answers: 2

Confusing about StringIO, cStringIO and ByteIO

Confusing about StringIO, cStringIO and ByteIO Question: 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 …

Total answers: 1

writing StringIO back to disk in python

writing StringIO back to disk in python Question: I created an in-memory zip file using StringIO and zipfile: inMemoryZip = StringIO() outfile = zipfile.ZipFile(inMemoryZip, ‘w’, compression=zipfile.ZIP_DEFLATED) //outfile.write(stuff) inMemoryZip.seek(0) return inMemoryZip This data is uploaded to a server/database. At some point, it’s retrieved and I need to write it to disk as a zip file. I’m …

Total answers: 1

Python Flask send_file StringIO blank files

Python Flask send_file StringIO blank files Question: I want to process a Pandas dataframe and send it to download as a CSV without a temp file. The best way to accomplish this I’ve seen is to use StringIO. Using the code below, a file downloads with the proper name, however the file is completely blank, …

Total answers: 4

Python3 error: initial_value must be str or None, with StringIO

Python3 error: initial_value must be str or None, with StringIO Question: While porting code from python2 to 3, I get this error when reading from a URL TypeError: initial_value must be str or None, not bytes. import urllib import json import gzip from urllib.parse import urlencode from urllib.request import Request service_url = ‘https://babelfy.io/v1/disambiguate’ text = …

Total answers: 4

How can I resolve TypeError with StringIO in Python 2.7?

How can I resolve TypeError with StringIO in Python 2.7? Question: Trying to read following string as file using StringIO but getting the error below. How can I resolve it? >> from io import StringIO >>> >>> datastring = StringIO(“”” … Country Metric 2011 2012 2013 2014 … USA GDP 7 4 0 2 … …

Total answers: 2