Python 类型错误:没有编码的字符串参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51961386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: string argument without an encoding
提问by Programmer120
I want to upload compressed gzip of Json into Google Storage.
我想将 Json 的压缩 gzip 上传到 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.
这create_jsonlines(source)
是一个返回 Json 换行符分隔的函数。
Running this code gives:
运行此代码给出:
TypeError: string argument without an encoding
The Python docssays the format is: bytes([source[, encoding[, errors]]])
I'm not sure I understand it as there is no example of how to use it.
在Python文档说,格式是:bytes([source[, encoding[, errors]]])
我不知道我把它理解为不存在如何使用它的例子。
I tried also
我也试过
bytes([(create_jsonlines(source))[,encoding='utf8']])
This gives :
这给出了:
SyntaxError: invalid syntax
I'm running Python 3.5
我正在运行 Python 3.5
回答by Black Thunder
You are not using the bytes
function correctly. Check this:
您没有bytes
正确使用该功能。检查这个:
>>> 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.
encoding
是bytes
函数的参数,并且您在该函数之外使用它。
回答by lincr
You are probably only one step away from the answer.
你可能离答案只有一步之遥。
See bytesarray()and bytesfor the function usage (you may need to change python version of the document).
有关函数用法,请参阅bytesarray()和bytes(您可能需要更改文档的 python 版本)。
And it says:
它说:
The optional source parameter can be used to initialize the array in a few different ways:
- If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode().
- If it is an integer, the array will have that size and will be initialized with null bytes.
- If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array.
- If it is an iterable, it must be an iterable of integers in the range 0 <= x < 256, which are used as the initial contents of the array.
可选的 source 参数可用于以几种不同的方式初始化数组:
- 如果是字符串,还必须给出编码(和可选的错误)参数;bytearray() 然后使用 str.encode() 将字符串转换为字节。
- 如果它是一个整数,则该数组将具有该大小并使用空字节进行初始化。
- 如果是符合buffer接口的对象,则使用该对象的只读缓冲区来初始化bytes数组。
- 如果是可迭代的,则必须是 0 <= x < 256 范围内的整数的可迭代,这些整数用作数组的初始内容。
Notice that the square bracket indicates that those parameters can be omitted, it is not an array type of python language.
注意方括号表示这些参数可以省略,它不是python语言的数组类型。
So you should use bytes(create_jsonlines(source), encoding='utf8')
.
所以你应该使用bytes(create_jsonlines(source), encoding='utf8')
.
回答by Nitin Kr
When you read any python function docs as
当您阅读任何 python 函数文档时
bytes([source[, encoding[, errors]]])
square brackets represent that those parameters are optional. multiple square brackets inside another mean they are next level of option params. For example
方括号表示这些参数是可选的。另一个中的多个方括号意味着它们是下一级选项参数。例如
bytes([source....
means we can call bytes as byes() itself as [source]
is optional here
意味着我们可以将字节称为 byes() 本身,这[source]
是可选的
bytes() -> empty bytes object
bytes(22)
Here 22 is being passed as the source
这里 22 被作为源传递
read this for more details about bytes and its params
阅读本文以了解有关字节及其参数的更多详细信息