How to read the file.xls obtained from the internet?
I'm trying to read the file .xls obtained from the internet using requests
to later turn it into an array or list of dictionaries. You need two columns from the file, without analyzing the data.
I tried it like this:
import requests as r
import xlrd
data = r.get(‘ https://zniis.ru/router/router-13_05_2020.xls’)
book = xlrd.open_workbook(data)
I am told that data
is of type response
, and is not readable.
Who has already done this, how to do parvilno?
Now I tried it like this, it works:
import requests as r
import xlrd
data = r.get('https://zniis.ru/router/router-14_05_2020.xls', verify=False)
with open('router-14_05_2020.xls', 'wb') as f:
f.write(data.content)
book = xlrd.open_workbook('router-14_05_2020.xls')
sheet = book.sheet_by_index(0)
But the problem is that I still save the file.
In general, now the question is how to feed the xld
type bytes
to the library so that it sees it .xls?
2 answers
You can pass a parameter to the xlrd.open_workbook function file_contents
(source code):
File_contents – A string or an mmap.mmap object or some other behave-alike object. If file_contents is supplied, filename will not be used, except (possibly) in messages.
I think it will be like this:
import requests as r
import xlrd
rs = r.get('https://zniis.ru/router/router-14_05_2020.xls', verify=False)
book = xlrd.open_workbook(file_contents=rs.content)
sheet = book.sheet_by_index(0)
Discover the Pandas module-it is ideal for processing, analyzing, and visualizing tabular data.
import pandas as pd # pip install pandas # conda install pandas
df = pd.read_excel("https://zniis.ru/router/router-13_05_2020.xls")
Then you can work with the read data:
In [6]: df
Out[6]:
Код DEF От До Емкость (всего номеров) Оператор связи Идентификатор региона MNC \
0 900 0 61999 62000 "Т2 Мобайл" ООО 25 20
1 900 62000 62999 1000 "Т2 Мобайл" ООО 62 20
2 900 63000 99999 37000 "Т2 Мобайл" ООО 25 20
3 900 100000 199999 100000 "Т2 Мобайл" ООО 70 20
4 900 200000 299999 100000 "Т2 Мобайл" ООО 75 20
... ... ... ... ... ... ... ...
7783 999 9480000 9489999 10000 "СИМ ТЕЛЕКОМ" ООО 77 47
7784 999 9490000 9499999 10000 "МегаФон" ПАО 14 2
7785 999 9500000 9599999 100000 "СИМ ТЕЛЕКОМ" ООО 77 47
7786 999 9600000 9799999 200000 "Скартел" ООО 77 11
7787 999 9800000 9999999 200000 "Скартел" ООО 77 11
Маршрутный номер
0 D2520
1 D6220
2 D2520
3 D7020
4 D7520
... ...
7783 D7747
7784 D1402
7785 D7747
7786 D7711
7787 D7711
[7788 rows x 8 columns]