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

2023-11-07Python开发问题
5

本文介绍了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')

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

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

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

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10