TypeError: string argument without an encoding

Programmer120 picture Programmer120 · Aug 22, 2018 · Viewed 48.5k times · Source

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

Answer

Black Thunder picture Black Thunder · Aug 22, 2018

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.