【问题标题】:Reading a pandas pickle file in Tensorflow in CloudML在 CloudML 中的 Tensorflow 中读取 pandas pickle 文件
【发布时间】:2018-10-05 17:10:09
【问题描述】:

我在尝试读取熊猫泡菜时遇到错误,例如df.to_pickle() 方法,存储在 Google Cloud 存储中。我正在尝试执行以下操作:

path_to_gcs_file = 'gs://xxxxx'
f = file_io.FileIO(path_to_gcs_file, mode='r').read()
train_df = pd.read_pickle(f)
f.close()

我收到以下错误:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte

我也试过了:

f = BytesIO(file_io.read_file_to_string(path_to_gcs_file, binary_mode=True))
train_df = pd.read_pickle(f)

在本地有效,但在 CloudML 上无效!

f = file_io.read_file_to_string(path_to_gcs_file, binary_mode=True)
train_df = pd.read_pickle(f)

给我一​​个错误: AttributeError: 'bytes' 对象没有属性 'seek'

【问题讨论】:

    标签: python pandas tensorflow google-cloud-ml


    【解决方案1】:

    您应该能够摆脱使用上下文管理器,但我认为您正在使用这种方式提取证书的末尾,因此您应该通过 api 下载文件

    pip install --upgrade google-cloud-storage
    

    然后

    # Initialise a client
    storage_client = storage.Client("[Your project name here]")
    # Create a bucket object for our bucket
    bucket = storage_client.get_bucket(bucket_name)
    # Create a blob object from the filepath
    blob = bucket.blob("folder_one/foldertwo/filename.extension")
    # Download the file to a destination
    blob.download_to_filename(path_to_gcs_file)
    with open(path_to_gcs_file, "rb" as f:
        train_df = = pickle.load(f)
    

    从这个答案中得到了很多: Downloading a file from google cloud storage inside a folder

    【讨论】:

    • 我收到 UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 6488: ordinal not in range(128).我猜可能是python版本问题?
    • 我也在使用df.to_pickle() 来创建泡菜
    • 你试过上面的方法了吗?如果原始转储对象是 df,pickle.load() 将返回一个 df 对象
    • 是的,你的确切代码给了我我评论的错误
    • 我查看了字节,它是“MHhmZg==”,看起来像是证书的结尾。您是否也有可能获得安全证书?你能用原始的 test.txt 文件试试吗?
    【解决方案2】:

    pandas.read_pickle 接受路径作为第一个参数;您正在传递一个 File 对象 (file.FileIO) 和一个 bytes 对象 (read_to_string)。

    到目前为止,我还没有找到使用 pandas 直接从 GCS 读取泡菜对象的方法,因此您必须将其复制到机器上。您可以为此使用file_io.copy

    file_io.copy('gs://xxxx', '/tmp/x.pkl')
    train_df = pd.read_pickle('/tmp/x.pkl')
    

    【讨论】:

    • 我收到错误:ImportError: No module named index.base when reading the pickle
    • 我也在尝试:` f = file_io.FileIO(path_to_gcs, mode='r')` 和 train_df = pickle.load(f) 它也不起作用...UnicodeDecodeError: 'utf-8' codec无法解码位置 0 的字节 0x80:无效的起始字节
    • 你试过mode='rb'吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-04
    • 1970-01-01
    相关资源
    最近更新 更多