类型错误:没有编码的字符串参数 [英] TypeError: string argument without an encoding

查看:28
本文介绍了类型错误:没有编码的字符串参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 Json 的压缩 gzip 上传到 Google Storage.

我有这个代码:

import datalab.storage 作为存储导入 gzip路径 = 前缀 + '/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 的函数.

运行此代码给出:

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

Python 文档 说格式是:bytes([source[, encoding[, errors]]]) 我不确定我是否理解它,因为没有如何使用它的示例.

我也试过

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

这给出:

SyntaxError: 无效语法

我正在运行 Python 3.5

解决方案

您没有正确使用 bytes 函数.检查这个:

<预><代码>>>>a = "你好">>>字节(一,编码='utf8')嘿嘿

你可以试试:

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

encodingbytes 函数的参数,您在该函数之外使用它.

I want to upload compressed gzip of Json into Google Storage.

I have this code:

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')

The create_jsonlines(source) is a function that returns Json Newline Delimited.

Running this code gives:

TypeError: string argument without an encoding

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.

I tried also

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

This gives :

SyntaxError: invalid syntax

I'm running Python 3.5

解决方案

You are not using the bytes function correctly. Check this:

>>> a = "hi"
>>> bytes(a, encoding='utf8')
b'hi'

You can try:

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

encoding is the argument of the bytes function, and you are using it outside of that function.

这篇关于类型错误:没有编码的字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆