4

I'm new to python and having trouble dealing with excel manpulation in python.

So here's my situation: I'm using requests to get a .xls file from a web server. After that I'm using xlrd to save the content in excel file. I'm only interested in one value of that file, and there are thousands of files im retrieving from different url addresses.

I want to know how could i handle the contents i get from request in some other way rather than creating a new file.

Besides, i've included my code my comments on how could I improve it. Besides, it doesn't work, since i'm trying to save new content in an already created excel file (but i couldnt figure out how to delete the contents of that file for my code to work (even if its not efficient)).

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

2 Answers 2

5

xlrd.open_workbook can accept a string for the file data instead of the file name. Your code could pass the contents of the XLS, rather than creating a file and passing its name.

Try this:

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

Reference: xlrd.open_workbook documentation

Sign up to request clarification or add additional context in comments.

1 Comment

pd.read_excel(xlrd.open_workbook(file_contents=resp.content), engine='xlrd') can also be used to directly transform it into a pandas DataFrame.
-1

Save it and then delete the file readily on each loop after the work with os.

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

1 Comment

The question states 'without saving file', and also why not use the tempfile module to create a temporary file if you are going to save it? That module can even help with cleaning up after the file was used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.