Error when using zlib.compress function in Python 3.2 - python-3.x

I'm importing zlib in my Python program. It works fine in Python 2.6 but shows an error when I try to run it in Python 3.2.
This is my code:
import zlib
s = 'sam'
print ("Your string length is",len(s))
t = zlib.compress(s)
print ("Your compressed string is",t)
print ("Your compressed string length is",len(t))
print ("Your decompressed string is",zlib.decompress(t))
print ("Crc32 is",zlib.crc32(t))
The error I get is this:
Your string length is 3
Traceback (most recent call last):
File "F:\workspace\samples\python\zip.py", line 4, in <module>
t = zlib.compress(s)
TypeError: 'str' does not support the buffer interface
But the above program works fine in Python 2.6. Should I use an alternative to zlib? Please help me.
Edit: I got it to work. It seems I needed to encode it. Here is the revised code:
import zlib
s = 'sam'
print ("Your string length is",len(s))
s=s.encode('utf-8')
t = zlib.compress(s)
print ("Your compressed string is",t)
print ("Your compressed string length is",len(t))
print ("Your decompressed string is",zlib.decompress(t))
print ("Crc32 is",zlib.crc32(t))

Th str type in Python is no longer a sequence of 8-bit characters, but a sequence of Uncode characters. You need to use the bytes type for binary data. You convert between strings and bytes by encoding/decoding.

Related

how can i open a random text file in python?

i am trying to make a python program that randomly selects a text file to open and outputs the contents of the randomly selected text file
when i try running the code, i get this error
Traceback (most recent call last):
File "//fileserva/home$/K59046/Documents/project/project.py", line 8, in
o = open(text, "r")
TypeError: expected str, bytes or os.PathLike object, not tuple
this is the code that i have written
import os
import random
os.chdir('N:\Documents\project\doodoo')
a = os.getcwd()
print("current dir is",a)
file = random.randint(1, 4)
text = (file,".txt")
o = open(text, "r")
print (o.read())
can somebody tell me what i am doing wrong?
As your error message says, your text variable is a tuple, not a string. You can use f-strings or string concatenation to solve this:
# string concatenation
text = str(file) + ".txt"
# f-strings
text = f"{file}.txt"
Your variable text is not what you expect. You currently create a tuple that could look like this: (2, ".txt"). If you want a string like "2.txt", you need to concatenate the two parts:
text = str(file) + ".txt"

How to hash a variable with sha256

I have been working on a program that allows you to enter any text you want and it will return the hashed result in sha256. However i am receiving an error on line 4 The whole error message:
Traceback (most recent call last):
File "main.py", line 4, in <module>
hash_object = hashlib.sha256(password_sign_up)
TypeError: Unicode-objects must be encoded before hashing
The code:
import hashlib
password_sign_up = input("Enter your desired password: ")
hash_object = hashlib.sha256(password_sign_up)
hex_dig = hash_object.hexdigest()
print(hex_dig)
You have to use .encode('utf-8') for your password.
In python 2x, the default encoding for any string is unicode. But in 3x, you will have to encode it to your choice of encoding. e.g. utf-32, utf-8 etc.
Try this: hash_object = hashlib.sha256(password_sign_up.encode('utf-8'))
You're taking the result of the input() function (which returns a str object) and putting it directly into sha256(). The sha256() function requires its parameter to be a bytes object.
You can convert a string to a bytes with:
myNewBytesObject = password_sign_up.encode('utf-8')

Fixing AttributeError: 'file' object has no attribute 'buffer' (Python3)

Python 2.7 on Ubuntu. I tried run small python script (file converter) for Python3, got error:
$ python uboot_mdb_to_image.py < input.txt > output.bin
Traceback (most recent call last):
File "uboot_mdb_to_image.py", line 29, in <module>
ascii_stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='ascii', errors='strict')
AttributeError: 'file' object has no attribute 'buffer'
I suspect it's caused by syntax differences between python 3 and python 2, here is script itself:
#!/usr/bin/env python3
import sys
import io
BYTES_IN_LINE = 0x10 # Number of bytes to expect in each line
c_addr = None
hex_to_ch = {}
ascii_stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='ascii', errors='strict')
for line in ascii_stdin:
line = line[:-1] # Strip the linefeed (we can't strip all white
# space here, think of a line of 0x20s)
data, ascii_data = line.split(" ", maxsplit = 1)
straddr, strdata = data.split(maxsplit = 1)
addr = int.from_bytes(bytes.fromhex(straddr[:-1]), byteorder = 'big')
if c_addr != addr - BYTES_IN_LINE:
if c_addr:
sys.exit("Unexpected c_addr in line: '%s'" % line)
c_addr = addr
data = bytes.fromhex(strdata)
if len(data) != BYTES_IN_LINE:
sys.exit("Unexpected number of bytes in line: '%s'" % line)
# Verify that the mapping from hex data to ASCII is consistent (sanity check for transmission errors)
for b, c in zip(data, ascii_data):
try:
if hex_to_ch[b] != c:
sys.exit("Inconsistency between hex data and ASCII data in line (or the lines before): '%s'" % line)
except KeyError:
hex_to_ch[b] = c
sys.stdout.buffer.write(data)
Can anyone advice how to fix this please?
It's an old question, but since I've run into a similar issue and it came up first when googling the error...
Yes, it's caused by a difference between Python 3 and 2. In Python 3, sys.stdin is wrapped in io.TextIOWrapper. In Python 2 it's a file object, which doesn't have a buffer attribute. The same goes for stderr and stdout.
In this case, the same functionality in Python 2 can be achieved using codecs standard library:
ascii_stdin = codecs.getreader("ascii")(sys.stdin, errors="strict")
However, this snippet provides an instance of codecs.StreamReader, not io.TextIOWrapper, so may be not suitable in other cases. And, unfortunately, wrapping Python 2 stdin in io.TextIOWrapper isn't trivial - see Wrap an open stream with io.TextIOWrapper for more discussion on that.
The script in question has more Python 2 incompabilities. Related to the issue in question, sys.stdout doesn't have a buffer attribute, so the last line should be
sys.stdout.write(data)
Other things I can spot:
str.split doesn't have maxsplit argument. Use line.split(" ")[:2] instead.
int doesn't have a from_bytes attribute. But int(straddr[:-1].encode('hex'), 16) seems to be equivalent.
bytes type is Python 3 only. In Python 2, it's an alias for str.

Decompress gz compressed string using Python3.6

I want to decompress the following gz compressed string using python3.6:
H4sIAAAAAAAA//NIzcnJVwjPL8pJAQBWsRdKCwAAAA==
The decompressed string is "Hello World"
I was able to decompress it using online tool - http://www.txtwizard.net/compression but I couldn't find a proper way to do it in python.
I tried zlib and gzip, but they require bytes not str. I also tried converting it using io.Bytes() but of no use. My Code is:
import gzip
import io
class SearchEvents:
def decompressPayload():
payload = "H4sIAAAAAAAA//NIzcnJVwjPL8pJAQBWsRdKCwAAAA=="
payload_bytes = io.BytesIO(payload)
print(gzip.decompress(payload_bytes))
SearchEvents.decompressPayload()
I am expecting "Hello World" as output. But I am getting the following error:
Traceback (most recent call last):
File "SearchEvents.py", line 13, in <module>
SearchEvents.decompressPayload()
File "SearchEvents.py", line 10, in decompressPayload
payload_bytes = io.BytesIO(payload)
TypeError: a bytes-like object is required, not 'str'
Is there any way to achieve what I want?
I want to decompress the following gz compressed string using python3.6:
...==
That's not a gzip-compressed string. At least, not until you Base64-decode it first.
>>> gzip.decompress(base64.b64decode('H4sIAAAAAAAA//NIzcnJVwjPL8pJAQBWsRdKCwAAAA=='))
b'Hello World'
For stuff that needs bytes, give it bytes. Add the b prefix to make a bytes literal, e.g.:
gzip.decompress(b"H4sIAAAAAAAA//NIzcnJVwjPL8pJAQBWsRdKCwAAAA==")
This doesn't work, because that's not valid compressed data. It looks like it's base64 encoded though, so by combining it with binascii, you get:
import binascii
import gzip
gzip.decompress(binascii.a2b_base64(b"H4sIAAAAAAAA//NIzcnJVwjPL8pJAQBWsRdKCwAAAA=="))
Which produces b'Hello World'

Python 3.2 TypeError - can't figure out what it means

I originally put this code through Python 2.7 but needed to move to Python 3.x because of work. I've been trying to figure out how to get this code to work in Python 3.2, with no luck.
import subprocess
cmd = subprocess.Popen('net use', shell=True, stdout=subprocess.PIPE)
for line in cmd.stdout:
if 'no' in line:
print (line)
I get this error
if 'no' in (line):
TypeError: Type str doesn't support the buffer API
Can anyone provide me with an answer as to why this is and/or some documentation to read?
Much appreciated.
Python 3 uses the bytes type in a lot places where the encoding is not clearly defined. The stdout of your subprocess is a file object working with bytes data. So, you cannot check if there is some string within a bytes object, e.g.:
>>> 'no' in b'some bytes string'
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
'no' in b'some bytes string'
TypeError: Type str doesn't support the buffer API
What you need to do instead is a test if the bytes string contains another bytes string:
>>> b'no' in b'some bytes string'
False
So, back to your problem, this should work:
if b'no' in line:
print(line)

Resources