Possible Bug in codecs.decode using ROT13 - python

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.

Related

How to properly write an 'If in' statement for an encoded string?

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'

Base64 encoding in python3

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.

An Error: 'Numpy.str_' object has no attribute 'decode'

I tried to run a test on Crab(an open source recommender system) based on python3. Then an error occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/Dennis/anaconda/lib/python3.5/site-packages/scikits/crab/datasets/base.py", line 201, in load_sample_movies
data_songs[u_ix][i_ix] = float(rating)
ValueError: could not convert string to float: "b'3.0'"
I tried to use 'decode()' to convert the string, but it's not working:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/Dennis/anaconda/lib/python3.5/site-packages/scikits/crab/datasets/base.py", line 202, in load_sample_movies
rating = rating.decode('utf-8')
AttributeError: 'numpy.str_' object has no attribute 'decode'
Any help will be appreciated!
The problem is that rating is a string within a string, so when you try casting a string like "b'3.0'" into a float, it gives a valueError because you still have the b in front which cannot be converted into float.
I imagine you need the byte encoding in front of the '3.0', so one way would be to evaluate rating to convert it from a string to bytes before typecasting it into a float (beware though, eval can have some safety issues).
>>> type(eval(rating))
<class 'bytes'>
>>> data_songs[u_ix][i_ix] = float(eval(rating))

Any ideas how to fix TypeError: 'str' does not support the buffer interface?

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)

Error in opening of file. Python

I trying to open file and met some problem:
TypeError: coercing to Unicode: need string or buffer, NoneType found
Here is the code example:
a = open(fname, "rb").read(255)
Whats wrong with the code?
fname is None, not a string:
>>> open(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, NoneType found
You'll have to fix how you set fname or explicitly guard against it being None.

Categories