I have an Excel file created inside a cStringIO variable.
I need to open it and read it. But to open an excel file with the xlrd function xlrd.open_workbook(excel_file_name), I need to call it by its file name. But in this case there is no file name because it is a cStrinIO variable that contains the representation of the Excel file.
How can I convert the cStringIO variable into a real excel file that I can open?
Thank you!
Looks like xlrd.open_workbook() accepts file_contents argument as well, so maybe as follows?
xlrd.open_workbook(file_contents=cstringio_var.getvalue())
You can use tempfile.NamedTemporaryFile.
Example (not tested):
with tempfile.NamedTemporaryFile() as f:
f.write(your_cStringIO_variable.read())
f.flush()
something = xlrd.open_workbook(f.name)
Related
am new to Python and working a bit on pickle files.
I have already a pickle file called training.pickle and a txt file called danish.txt
I would like to import the danish.txt to the training.pickle file but i don't know how to do ?
I have tried some thing but am sure its wrong :-)
import pickle
file1=open('danish.txt','r')
file2=open('training.pickle','r')
obj=[file1.read(), file2.read()]
outfile.write("obj,training.pickle")
I don't know much about pickle but if you're just trying to add the data from "danish.txt" to the pickle file you should be able to just open the .txt, store the data in a variable, and then write the data in the pickle.
To demonstrate my thinking:
f = open("danish.txt", "r+")
data = f.read()
output = data
f.close() #this reads the .txt file
and then afterward you'd write "output" into the pickle file via whatever method you use to write a string variable to a pickle file.
P.S. as I said I don't know much about pickle, but if it works anything like writing to a .txt you'd have to change the r to a w because r means opening it in read mode. If its just reading it can't write, or atleast that's how it works with .txts. Also, if there's no particular reason why you're using a pickle to store data, why not just use a .txt?
Does anyone know how to clear a file's contents on python?
Thank you.
Opening a file creates it and (unless append ('a') is set) overwrites it with emptyness, such as this:
open(filename, 'w').close()
See: How to empty a file using Python
You can use truncate function on the file object:
file_object = open('test.txt','r+')
file_object.truncate()
file_object.close()
I'm not sure how to word my question exactly, and I have seen some similar questions asked but not exactly what I'm trying to do. If there already is a solution please direct me to it.
Here is what I'm trying to do:
At my work, we have a few pkgs we've built to handle various data types. One I am working with is reading in a csv file into a std_io object (std_io is our all-purpose object class that reads in any type of data file).
I am trying to connect this to another pkg I am writing, so I can make an object in the new pkg, and covert it to a std_io object.
The problem is, the std_io object is meant to read an actual file, not take in an object. To get around this, I can basically write my data to temp.csv file then read it into a std_io object.
I am wondering if there is a way to eliminate this step of writing the temp.csv file.
Here is my code:
x #my object
df = x.to_df() #object class method to convert to a pandas dataframe
df.to_csv('temp.csv') #write data to a csv file
std_io_obj = std_read('temp.csv') #read csv file into a std_io object
Is there a way to basically pass what the output of writing the csv file would be directly into std_read? Does this make sense?
The only reason I want to do this is to avoid having to code additional functionality into either of the pkgs to directly accept an object as input.
Hope this was clear, and thanks to anyone who contributes.
For those interested, or who may have this same kind of issue/objective, here's what I did to solve this problem.
I basically just created a temporary named file, linked a .csv filename to this temp file, then passed it into my std_read function which requires a csv filename as an input.
This basically tricks the function into thinking it's taking the name of a real file as an input, and it just opens it as usual and uses csvreader to parse it up.
This is the code:
import tempfile
import os
x #my object I want to convert to a std_io object
text = x.to_df().to_csv() #object class method to convert to a pandas dataframe then generate the 'text' of a csv file
filename = 'temp.csv'
with tempfile.NamedTemporaryFile(dir = os.path.dirname('.')) as f:
f.write(text.encode())
os.link(f.name, filename)
stdio_obj = std_read(filename)
os.unlink(filename)
del f
FYI - the std_read function essentially just opens the file the usual way, and passes it into csvreader:
with open(filename, 'r') as f:
rdr = csv.reader(f)
I am working with a library that wants me to pass it data in the form of a file name. Then it will open that file and read the data. I have the data in a string, and I don't want to write it to a file (because I don't want to have to delete it afterwards).
Is there a way I can convert the string to a stream and generate a file name that will allow my library to open my stream and access the contents of my string?
import tempfile
fh = tempfile.NamedTemporaryFile() # this creates an actual file in the temp directory
fh.write(my_string)
print fh.name
call_other_thing(fh.name)
fh.close() # file is now deleted
Is there a way to pass the contents of an Excel file, rather than a file name/reference, to the xlrd module's open_workbook() function? I've been trying to use the "file_contents" parameter for this purpose, but haven't had any success with it so far. Thank you.
#Ber's comment is correct. You will need to use the getvalue() method of the StringIO object and pass that to the file_content parameter in the function call.
f = StringIO.StringIO(content)
book = xlrd.open_workbook(file_contents = f.getvalue() )
In most places where an open file is needed, a StringIO object will also work.
You just cread a StringIO object from the file data and pass that object as the file to your function.