Convert zip file to bytes in python - python

Goal for question - open a zip file and convert it into a bytes-like object.
When I tried I get the error:
encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'list'
Here is the code:
import base64
with open("results.zip", 'rb') as f:
data = f.readlines()
print(data)
encoded = base64.b64encode(data)
I also tried this and got the same exact error:
import zipfile
with open("results.zip", 'rb') as f:
data = f.readlines()
zf = zipfile.ZipFile(io.BytesIO(data), "r")
for fileinfo in zf.infolist():
print(zf.read(fileinfo).decode('ascii'))

Thanks to #Vlad for his comment as it helped me to get the answer.
import base64
with open("results.zip", 'rb') as f:
data = f.read()
print(data)
encoded = base64.b64encode(data)

Related

Problem to convert act format to CSV a bytes-like object is required, not 'str'

I have a problem converting an .act file to csv format. I am using this code:
import struct
import csv
DATA = []
with open("modis_lst.act", "rb") as actFile:
for _ in range(256):
raw = actFile.read(3)
color = struct.unpack("3B", raw)
DATA.append(color)
with open('test.csv', 'wb') as csvfile:
csvWriter = csv.writer(csvfile)
csvWriter.writerows(DATA)
but I get the following ERROR:
a bytes-like object is required, not 'str'
I mocked up what I think you're working with:
import struct
import csv
DATA = []
raw = struct.pack("3B", 255, 0, 0)
color = struct.unpack("3B", raw)
DATA.append(color)
with open("test.csv", "wb") as csvfile:
csvWriter = csv.writer(csvfile)
csvWriter.writerows(DATA)
and I too get:
File "/users/z/main.py", line 15, in <module>
csvWriter.writerows(DATA)
TypeError: a bytes-like object is required, not 'str'
And this is because opening the file as binary (wb) is wrong in this case: you've already decoded the binary structure into strings, so open the file as plain text and pass that to the CSV writer:
with open("test.csv", "w") as csvfile:
and now my test.csv file looks correct:
255,0,0
P.S. I don't know when opening a file as binary for writing CSV is correct... I've never thought of that before :)

How to create a csv file virtually without any physical file / path defined?

Need to create a csv file and convert it into byte like data for sending as EDI doc. I am trying to achieve this without having a physical file because location/path is unknown. Let me know if there is anyway we could achieve.
with open(
"/home/some path/*.dat", "r+", newline="\n"
) as write_f:
data_file = csv.writer(write_f, delimiter=';')
header_vals = ["header values"]
query = """data fetching query"""
data_file.writerow(header_vals)
self.env.cr.execute(query)
data_vals = self.env.cr.fetchall()
data_file.writerows(data_vals)
po_data = write_f.read(1024)
return po_data
Try 1: Instead of path, tried IO objects(BytesIO/StringIO)
data_file = BytesIO()
data_write = csv.writer(data_file, delimiter=';')
header_vals = ["header values"]
query = """data fetching query"""
data_write.writerow(header_vals)
self.env.cr.execute(query)
data_vals = self.env.cr.fetchall()
data_write.writerows(data_vals)
Received the error at writerow: TypeError: a bytes-like object is required, not 'str'
BytesIO behaves like a file in binary (!) mode. You need to write bytes to it.
But a csv.writer cannot write bytes, it only writes strings. That's the error message you see.
from io import StringIO
buffer = StringIO()
writer = csv.writer(buffer, delimiter=';')
header_vals = ['column_1', 'column_2']
writer.writerow(header_vals)
print(buffer.getvalue())
# => 'column_1;column_2\r\n'

Bytes-like object is required, not 'str' ud120

import sys
sys.path.append("C:/Users/peter/ud1200/ud120-projects/tools/")
sys.path.append('C:/Users/peter/ud1200/ud120-projects/choose_your_own')
sys.path.append('C:/Users/peter/ud1200/ud120-projects/datasets_questions')
import os
os.chdir('C:/Users/peter/ud1200/ud120-projects/datasets_questions')
import pickle
I tried also this solution
original = "C:/Users/peter/ud1200/ud120-projects/final_project/final_project_dataset.pkl"
destination = "C:/Users/peter/ud1200/ud120-projects/final_projec/final_project_dataset_unix.pkl"
content = ''
outsize = 0
with open(original, 'rb') as infile:
content = infile.read()
with open(destination, 'wb') as output:
for line in content.splitlines():
outsize += len(line) + 1
output.write(line + str.encode('\n'))
print("Done. Saved %s bytes." % (len(content)-outsize))
enron_data = pickle.load(open("C:/Users/peter/ud1200/ud120-projects/final_project/final_project_dataset.pkl", "rb"))
When i used this reference destination file reads there's 81 people in Enron dataset
There's another reference i used This solution
enron_data = pickle.load(open("../final_project/final_project_dataset.pkl", "r"))
print ('Number of people in the Enron dataset: {0}'.format(len(enron_data)))
But this solution produces the TypeError: a bytes-like object is required, not 'str' although the original solution found the right number of mails
print ('Number of people in the Enron dataset: {0}'.format(len(destination)))
Any help!!!

How to base64 encode a Python script

I'm trying to base64 encode a python file then insert it into a txt file I've tried the following code:
import base64
file = open('Python.py', 'r')
txt = open('New.txt', 'wb')
encoded = base64.b64encode(file).read()
txt.write(encoded)
txt.writelines(lines)
file.close()
txt.close()
The error returned is TypeError: a bytes-like object is required, not '_io.TextIOWrapper'
Here's a short Python 3 program that encodes any binary file to Base64.
base64encode.py
#!/usr/bin/env python3
import base64
import sys
def main():
if len(sys.argv) != 2:
print('Encode a file with Base64\nUsage:\n%s filename' % sys.argv[0])
sys.exit()
fname = sys.argv[1]
with open(fname, 'rb') as f:
data = f.read()
with open(fname + 'b64', 'wb') as f:
f.write(base64.encodebytes(data))
if __name__ == '__main__':
main()
Here's what it produces when fed its own source code (which was saved as UTF-8).
base64encode.pyb64
IyEvdXNyL2Jpbi9lbnYgcHl0aG9uMwoKaW1wb3J0IGJhc2U2NAppbXBvcnQgc3lzCgpkZWYgbWFp
bigpOgogICAgaWYgbGVuKHN5cy5hcmd2KSAhPSAyOgogICAgICAgIHByaW50KCdFbmNvZGUgYSBm
aWxlIHdpdGggQmFzZTY0XG5Vc2FnZTpcbiVzIGZpbGVuYW1lJyAlIHN5cy5hcmd2WzBdKQogICAg
ICAgIHN5cy5leGl0KCkKCiAgICBmbmFtZSA9IHN5cy5hcmd2WzFdCiAgICB3aXRoIG9wZW4oZm5h
bWUsICdyYicpIGFzIGY6CiAgICAgICAgZGF0YSA9IGYucmVhZCgpCgogICAgd2l0aCBvcGVuKGZu
YW1lICsgJ2I2NCcsICd3YicpIGFzIGY6CiAgICAgICAgZi53cml0ZShiYXNlNjQuZW5jb2RlYnl0
ZXMoZGF0YSkpCgppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgogICAgbWFpbigpCg==
And here's some code that reverses the process
import base64
with open('base64encode.pyb64', 'rb') as f:
data = f.read()
with open('newfile', 'wb') as f:
f.write(base64.decodebytes(data))
The resulting newfile is identical to base64encode.py

Pickle: TypeError: a bytes-like object is required, not 'str' [duplicate]

This question already has answers here:
Using pickle.dump - TypeError: must be str, not bytes
(3 answers)
Closed 4 years ago.
I keep on getting this error when I run the following code in python 3:
fname1 = "auth_cache_%s" % username
fname=fname1.encode(encoding='utf_8')
#fname=fname1.encode()
if os.path.isfile(fname,) and cached:
response = pickle.load(open(fname))
else:
response = self.heartbeat()
f = open(fname,"w")
pickle.dump(response, f)
Here is the error I get:
File "C:\Users\Dorien Xia\Desktop\Pokemon-Go-Bot-Working-Hack-API-master\pgoapi\pgoapi.py", line 345, in login
response = pickle.load(open(fname))
TypeError: a bytes-like object is required, not 'str'
I tried converting the fname1 to bytes via the encode function, but It still isn't fixing the problem. Can someone tell me what's wrong?
You need to open the file in binary mode:
file = open(fname, 'rb')
response = pickle.load(file)
file.close()
And when writing:
file = open(fname, 'wb')
pickle.dump(response, file)
file.close()
As an aside, you should use with to handle opening/closing files:
When reading:
with open(fname, 'rb') as file:
response = pickle.load(file)
And when writing:
with open(fname, 'wb') as file:
pickle.dump(response, file)
In Python 3 you need to specifically call either 'rb' or 'wb'.
with open('C:\Users\Dorien Xia\Desktop\Pokemon-Go-Bot-Working-Hack-API-master\pgoapi\pgoapi.py', 'rb') as file:
data = pickle.load(file)
You need to change 'str' to 'bytes'. Try this:
class StrToBytes:
def __init__(self, fileobj):
self.fileobj = fileobj
def read(self, size):
return self.fileobj.read(size).encode()
def readline(self, size=-1):
return self.fileobj.readline(size).encode()
with open(fname, 'r') as f:
obj = pickle.load(StrToBytes(f))
I keep coming back to this stack overflow link, so I'm posting the real answer for the next time I come looking for it:
PickleDB is messed up and needs to be fixed.
Line 201 of pickledb.py
From:
simplejson.dump(self.db, open(self.loco, 'wb'))
to:
simplejson.dump(self.db, open(self.loco, 'wt'))
Problem solved forever.

Categories