Python: how to process excel data from network without saving file

I am new to python and have problems handling excel in python.

So here is my situation: I am using requests to get a .xls file from a web server. After that, I use xlrd to save the content in an excel file. I am only interested in one value of this file and there are thousands of files that are fetched from different urls.

I want to know how I can handle the content I receive from the request in some other way, and not create a new file.

Also, I have included my code in my comments on how to improve it. Also, it doesn't work as I'm trying to save new content in an already generated excel file (but I couldn't figure out how to remove the contents of that file for my code to work (even if it's ineffective)).

import requests
import xlrd
d={}
for year in string_of_years:
    for month in string_of_months:  
        dls=" http://.../name_year_month.xls"
        resp = requests.get(dls)
        output = open('temp.xls', 'wb')
        output.write(resp.content)
        output.close()
        workbook = xlrd.open_workbook('temp.xls')
        worksheet = workbook.sheet_by_name(mysheet_name)
        num_rows = worksheet.nrows
        for k in range(num_rows):
            if condition I'm looking for:
                w={key_year_month:worksheet.cell_value(k,0)}
                dic.update(w)
                break

      

+3


source to share


2 answers


xlrd.open_workbook

can accept a string for file data instead of a filename. Your code can pass XLS content rather than create a file and pass its name.

Try the following:



    # UNTESTED
    resp = requests.get(dls)
    workbook = xlrd.open_workbook(file_contents=resp.content)

      

Link: xlrd.open_workbook

documentation

+1


source


Save it and then delete the file every cycle after working with os.



import os
#Your Stuff here
os.remove(#path to temp_file)

      

0


source







All Articles