TypeError: string argument without an encoding(TypeError:没有编码的字符串参数)
问题描述
我想将 Json 的压缩 gzip 上传到 Google 存储中.
I want to upload compressed gzip of Json into Google Storage.
我有这个代码:
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 Newline Delimited 的函数.
The create_jsonlines(source)
is a function that returns Json Newline Delimited.
运行此代码给出:
TypeError: string argument without an encoding
Python docs 表示格式为:bytes([source[, encoding[, errors]]])
我不确定我是否理解它,因为没有如何使用它的示例.
The Python docs says the format is: bytes([source[, encoding[, errors]]])
I'm not sure I understand it as there is no example of how to use it.
我也试过了
bytes([(create_jsonlines(source))[,encoding='utf8']])
这给出了:
SyntaxError: invalid syntax
我正在运行 Python 3.5
I'm running Python 3.5
推荐答案
您没有正确使用 bytes
函数.检查这个:
You are not using the bytes
function correctly. Check this:
>>> a = "hi"
>>> bytes(a, encoding='utf8')
b'hi'
你可以试试:
bytes((create_jsonlines(source)), encoding='utf8')
encoding
是 bytes
函数的参数,您在该函数之外使用它.
encoding
is the argument of the bytes
function, and you are using it outside of that function.
这篇关于TypeError:没有编码的字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:TypeError:没有编码的字符串参数


- pytorch 中的自适应池是如何工作的? 2022-07-12
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 沿轴计算直方图 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01