从 Python 中的 Databricks Filestore 下载的 CSV 文件无法正常工作
CSV File download from Databricks Filestore in Python not working
我正在使用下面的 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>
使用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)
我正在使用下面的 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>
使用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)