Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:没有编码的字符串参数_Python_Google Cloud Platform_Google Cloud Storage_Google Cloud Datalab - Fatal编程技术网

Python TypeError:没有编码的字符串参数

Python TypeError:没有编码的字符串参数,python,google-cloud-platform,google-cloud-storage,google-cloud-datalab,Python,Google Cloud Platform,Google Cloud Storage,Google Cloud Datalab,我想把Json的压缩gzip上传到Google存储中 我有以下代码: import datalab.storage as storage import gzip path = prefix + '/orders_newline.json.gz' storage.Bucket('orders').item(path).write_to(gzip.compress(bytes(create_jsonlines(source)),encoding='utf8'), 'application/json'

我想把Json的压缩gzip上传到Google存储中

我有以下代码:

import datalab.storage as storage
import gzip
path = prefix + '/orders_newline.json.gz'
storage.Bucket('orders').item(path).write_to(gzip.compress(bytes(create_jsonlines(source)),encoding='utf8'), 'application/json')
create_jsonlines(source)
是一个返回Json换行符的函数

运行此代码将提供:

TypeError: string argument without an encoding
上面说的格式是:
字节([source[,encoding[,errors]]])
我不确定我是否理解它,因为没有关于如何使用它的示例

我也试过了

bytes([(create_jsonlines(source))[,encoding='utf8']])
这使得:

SyntaxError: invalid syntax

我正在运行Python3.5

您可能离答案只有一步之遥

有关函数用法,请参见和(您可能需要更改文档的python版本)

上面写着:

可选的source参数可用于在 几种不同的方式:

  • 如果它是一个字符串,您还必须提供编码(以及可选的错误)参数;然后,bytearray()将字符串转换为字节 使用str.encode()
  • 如果它是整数,则数组将具有该大小,并将使用空字节初始化
  • 如果它是符合缓冲区接口的对象,则该对象的只读缓冲区将用于初始化字节数组

  • 如果它是一个iterable,那么它必须是范围为0的整数的iterable您没有正确使用
    bytes
    函数。选中此项:

    >>> a = "hi"
    >>> bytes(a, encoding='utf8')
    b'hi'
    
    您可以尝试:

    bytes((create_jsonlines(source)), encoding='utf8')
    

    encoding
    bytes
    函数的参数,您在该函数之外使用它。

    当您将任何python函数文档作为

    字节([source[,encoding[,errors]])
    
    方括号表示这些参数是可选的。另一个方括号内的多个方括号表示它们是下一级选项参数。比如说

    bytes([source....
    
    这意味着我们可以将字节称为yes(),因为
    [source]
    在这里是可选的

    bytes() -> empty bytes object
    
    这里22被传递为源

    有关字节及其参数的更多详细信息,请阅读本文


    您只需执行
    str.encode()
    即可,无需指定编码。此即时代码示例的问题是编码参数的函数错误。
    bytes(22)