假设有一个excel-file
链接为http://www.gstatic.com/adwords-scripty/account_upload_template.xlsx
我想解析 Excel 文件,然后在请求链接后将数据保存到数据库中。
我尝试使用pandas.read_excel()
但失败了。抱歉,我是 pandas
如何才能达到预期效果。
excel_url = "http://www.gstatic.com/adwords-scripty/account_upload_template.xlsx"
csv_url = "http://www.gstatic.com/adwords-scripty/account_upload_template.csv"
import csv
import requests
import pandas as pd
from requests.sessions import Session
s = Session()
with s.get(excel_url) as res:
r = pd.read_excel(res.text)
print(r)
最佳答案
首先,read_excel
需要一个流或类似文件的对象,而不是字符串。其次,text
会将 Excel 字节解码为 UTF8,但这不一定安全。试试这个:
excel_url = "http://www.gstatic.com/adwords-scripty/account_upload_template.xlsx"
csv_url = "http://www.gstatic.com/adwords-scripty/account_upload_template.csv"
import io
import csv
import requests
import pandas as pd
from requests.sessions import Session
s = Session()
with s.get(excel_url) as res:
file_like = io.BytesIO(res.content)
r = pd.read_excel(file_like)
print(r)
关于python - 如何直接处理 excel-file-link PYTHOn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57815780/