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
When you read any python function docs as
square brackets represent that those parameters are optional. multiple square brackets inside another mean they are next level of option params. For example
means we can call bytes as byes() itself as
[source]
is optional hereHere 22 is being passed as the source
read this for more details about bytes and its params
https://docs.python.org/3/library/stdtypes.html#bytes
You are probably only one step away from the answer.
See bytesarray() and bytes for the function usage (you may need to change python version of the document).
And it says:
Notice that the square bracket indicates that those parameters can be omitted, it is not an array type of python language.
So you should use
bytes(create_jsonlines(source), encoding='utf8')
.You are not using the
bytes
function correctly. Check this:You can try:
encoding
is the argument of thebytes
function, and you are using it outside of that function.