my script includes this line:
encoded = "Basic " + s.encode("base64").rstrip()
But gives me back the error:
LookupError: 'base64' is not a text encoding; use codecs.encode() to handle arbitrary codecs
This line seemed to work fine in python 2 but since switching to 3 I get the error
This string codec was removed in Python 3. Use base64 module:
Python 3.6.1 (default, Mar 23 2017, 16:49:06)
>>> import base64
>>> base64.b64encode('whatever')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/base64.py", line 58, in b64encode
encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'
>>> base64.b64encode(b'whatever')
b'd2hhdGV2ZXI='
>>>
Don't forget to convert the data to bytes.
Code as follows:
base64.urlsafe_b64encode('Some String'.encode('UTF-8')).decode('ascii')
For example: return {'raw': base64.urlsafe_b64encode(message.as_string().encode('UTF-8')).decode('ascii')}
Worked for me.
Related
I have the following code:
import json
domain="abc.com"
rawlog = json.loads(
f'{"domain": ${domain}}')
print(rawlog["domain"])
Which gives me:
Traceback (most recent call last):
File "<string>", line 6, in <module>
ValueError: Invalid format specifier
>
The question is, what is the cause and if I can have fstring in Json? I'm using the newest Python: python3 --version shows Python 3.10.4.
I also tried:
import json
domain="abc.com"
rawlog = json.loads('{"domain": f'{domain}'}')
print(rawlog["domain"])
but it gives:
File "<string>", line 5
rawlog = json.loads('{"domain": f'domain'}')
^
SyntaxError: invalid syntax
As { and } have special meaning they need to be escaped to mean literal { and literal }, consider following simple example
import json
domain="abc.com"
string=f'{{"domain": "{domain}"}}'
parsed=json.loads(string)
print(parsed)
gives output
{'domain': 'abc.com'}
I'm trying to take the string hello, encode it into hexadecimal, and then print success if the value of t is found in the encoded string.
This is what I have currently:
import codecs
t='68656c6c6df'
pkts = ("hello")
pkts1 = codecs.encode(b'hello', 'hex_codec')
if "t" in pkts1:
print ('success')
Which gives me the error:
Traceback (most recent call last):
File "C:/Users/K/.PyCharmCE2018.1/config/scratches/scratch_1.py", line 8, in <module>
if "t" in pkts1:
TypeError: a bytes-like object is required, not 'str'
Python 3.5.1 on Ubuntu
>>> from codecs import decode
>>> s = 'string'
>>> b = b'bytes'
>>> decode(b, 'utf8')
'bytes'
>>> decode(s, 'utf8')
Traceback (most recent call last):
File "/usr/lib/python3.5/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
TypeError: a bytes-like object is required, not 'str'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: decoding with 'utf8' codec failed (TypeError: a bytes-like object is required, not 'str')
so far everything behaves as expected. But when I try to use ROT13 encoding I get:
>>> decode(s, 'rot13')
'fgevat'
>>> decode(b, 'rot13')
Traceback (most recent call last):
File "/usr/lib/python3.5/encodings/rot_13.py", line 18, in decode
return (input.translate(rot13_map), len(input))
TypeError: a bytes-like object is required, not 'dict'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: decoding with 'rot13' codec failed (TypeError: a bytes-like object is required, not 'dict')
As #snakecharmerb points out ROT13 en/decoding is only supposed to work on strings.
The exception Message though, is still wrong since it states, that a bytes-like object was expected even when a bytes-like object was actually passed, and mentions some dictionary the user did not create.
Regarding the ROT13 codec, the Python 3.5 docs state:
The following codec provides a text transform: a str to str mapping.
It is not supported by str.encode() (which only produces bytes
output).
that is, you can only pass unicode strings (str objects) when encoding to ROT13 and you will only get str objects back.
This is different from Python 2.x, when ROT13 was treated the same as other codecs. ROT13 was not ported to Python 3 initially, because ROT13 does not encode a unicode string to bytes - it just swaps letters around. It as reinstated, as a text transform in Python 3.2 and 3.4. The full story is in this bug report.
I keep getting this error "TypeError: 'str' does not support the buffer interface" Not sure what is going wrong. Any assistance would be great.
import zlib
#User input for sentnce & compression.
sentence = input("Enter the text you want to compress: ")
com = zlib.compress(sentence)
#Opening file to compress user input.
with open("listofwords.txt", "wb") as myfile:
myfile.write(com)
The error means that you are trying to pass str object (Unicode text) instead of binary data (byte sequence):
>>> import zlib
>>> zlib.compress('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' does not support the buffer interface
Python 3.5 improves the error message here:
>>> import zlib
>>> zlib.compress('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
To save text as binary data, you could encode it using a character encoding. To compress the data, you could use gzip module:
import gzip
import io
with io.TextIOWrapper(gzip.open('sentence.txt.gz', 'wb'),
encoding='utf-8') as file:
print(sentence, file=file)
Is this a bug?
>>> import json
>>> import cPickle
>>> json.dumps(cPickle.dumps(u'å'))
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/__init__.py", line 230, in dumps
return _default_encoder.encode(obj)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/encoder.py", line 361, in encode
return encode_basestring_ascii(o)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 1-3: invalid data
The json module is expecting strings to encode text. Pickled data isn't text, it's 8-bit binary.
One simple workaround, if you really need to send pickled data over JSON, is to use base64:
j = json.dumps(base64.b64encode(cPickle.dumps(u'å')))
cPickle.loads(base64.b64decode(json.loads(j)))
Note that this is very clearly a Python bug. Protocol version 0 is explicitly documented as ASCII, yet å is sent as the non-ASCII byte \xe5 instead of encoding it as "\u00E5". This bug was reported upstream--and the ticket was closed without the bug being fixed. http://bugs.python.org/issue2980
Could be a bug in pickle. My python documentation says (for used pickle format): Protocol version 0 is the original ASCII protocol and is backwards compatible with earlier versions of Python. [...] If a protocol is not specified, protocol 0 is used.
>>> cPickle.dumps(u'å').decode('ascii')
Traceback (most recent call last):
File "", line 1, in
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1: ordinal not in range(128)
that aint no ASCII
and, don't know whether its relevant, or even a problem:
>>> cPickle.dumps(u'å') == pickle.dumps(u'å')
False
I'm using Python2.6 and your code runs without any error.
In [1]: import json
In [2]: import cPickle
In [3]: json.dumps(cPickle.dumps(u'å'))
Out[3]: '"V\\u00e5\\np1\\n."'
BTW, what's your system default encoding, in my case, it's
In [6]: sys.getdefaultencoding()
Out[6]: 'ascii'