我正在使用下面的Python代码从Databricks Filestore下载一个csv文件。通常,当文件保存在Filestore中时,可以通过浏览器下载。
当我在浏览器中直接输入文件的url时,该文件可以下载。但是,当我试图通过下面的代码进行同样的操作时,下载的文件的内容不是csv,而是一些html代码--参见下面的代码。
下面是我的Python代码:
def download_from_dbfs_filestore(file):
url ="https://databricks-hot-url/files/{0}".format(file)
req = requests.get(url)
req_content = req.content
my_file = open(file,'wb')
my_file.write(req_content)
my_file.close()
这是html。它似乎是在引用登录页面,但不知道从这里该做什么:
<!doctype html><html><head><meta charset="utf-8"/>
<meta http-equiv="Content-Language" content="en"/>
<title>Databricks - Sign In</title><meta name="viewport" content="width=960"/>
<link rel="icon" type="image/png" href="/favicon.ico"/>
<meta http-equiv="content-type" content="text/html; charset=UTF8"/><link rel="icon" href="favicon.ico">
</head><body class="light-mode"><uses-legacy-bootstrap><div id="login-page">
</div></uses-legacy-bootstrap><script src="login/login.xxxxx.js"></script>
</body>
</html>
发布于 2021-05-24 12:24:52
使用base64模块b64decode解决问题
import base64
DOMAIN = <your databricks 'host' url>
TOKEN = <your databricks 'token'>
jsonbody = {"path": <your dbfs Filestore path>}
response = requests.get('https://%s/api/2.0/dbfs/read/' % (DOMAIN), headers={'Authorization': 'Bearer %s' % TOKEN},json=jsonbody )
if response.status_code == 200:
csv=base64.b64decode(response.json()["data"]).decode('utf-8')
print(csv)
https://stackoverflow.com/questions/67647608
复制相似问题