python - Snowflake python 表存储阶段文件

标签 python pandas snowflake-cloud-data-platform

我有以下小测试,我想在雪花的内部阶段存储 xlsx 文件:

import snowflake.snowpark as snowpark
import xlsxwriter
import pandas as pd
from snowflake.snowpark.functions import col

def main(session: snowpark.Session): 
# Your code goes here, inside the "main" handler.
tableName = 'information_schema.packages'
dataframe = session.table(tableName).filter(col("language") == 'python')

x = dataframe.to_pandas()
unload_location = "@JV_TEST/jv.xlsx"

x.to_excel(unload_location)
# Print a sample of the dataframe to standard output.
dataframe.show()

# Return value will appear in the Results tab.
return dataframe

但我收到以下错误:

Traceback (most recent call last):
  Worksheet, line 17, in main
  File "pandas/util/_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
  File "pandas/util/_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
  File "pandas/core/generic.py", line 2374, in to_excel
    formatter.write(
  File "pandas/io/formats/excel.py", line 944, in write
    writer = ExcelWriter(  # type: ignore[abstract]
  File "pandas/io/excel/_xlsxwriter.py", line 205, in __init__
    super().__init__(
  File "pandas/io/excel/_base.py", line 1313, in __init__
    self._handles = get_handle(
  File "pandas/io/common.py", line 734, in get_handle
    check_parent_directory(str(handle))
  File "pandas/io/common.py", line 597, in check_parent_directory
    raise OSError(rf"Cannot save file into a non-existent directory: '{parent}'")
OSError: Cannot save file into a non-existent directory: '@JV_TEST'

如何在阶段存储文件? (我想使用 pandas 和 xlsxwrite,因为我想将一些东西应用到 Excel 工作表)

最佳答案

目前无法使用 Python 工作表写入阶段。 Python 存储过程和 UDFs 的一般限制也适用于 Python 工作表。

这意味着您只能将文件写入 /tmp,一旦 session 终止,将无法再从 /tmp 访问文件。 您只能使用 Python 的内置函数(如 open())写入 /tmp,它不适用于 xlsxwriter 等外部函数。

作为一般规则,使用 Python 操作存储在舞台上的文件是不可能的。

关于python - Snowflake python 表存储阶段文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76482383/

相关文章:

python - 对 SQL 结果进行排序以提高可读性?

python - 如何在python中每次找到最近52周销售额的平均值

jdbc - 如何在 Snowflake JDBC 中执行批量更新

Python,安装 clarifai --> VS14.0 link.exe 失败,退出状态为 1158

python - 如何使用 Python 检索动态 html 内容的值

python - Qt 的多处理适用于 Windows 但不适用于 Linux

python - 在 Pandas 中的 groupby 之后查找与最大日期关联的行

python - 如何计算 python pandas 中的行而不是值?

snowflake-cloud-data-platform - 查找值列表中特定值的转换次数

jdbc - 使用 JDBC 将数据插入 Snowflake