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

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!!!

Related

Convert zip file to bytes in 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)

python 3.6: Error: a bytes-like object is required, not 'str' when reading a file

I am trying read a file with following code.
filenames = os.listdir(path)
data = []
for file in filenames:
file_path = os.path.join(path, file)
print (file_path)
with open(file_path, 'r') as f:
try:
soundId = os.path.splitext(file)[0]
print (soundId)
content = f.read()
pp = pickle.loads(content)
pp = np.asarray(pp)
data[soundId] = pp
except Exception as e:
print ("Error occurred" + str(e))
when i run the code it give me
Error occurreda bytes-like object is required, not 'str'
The error occurs at the line pp = pickle.loads(content)
There are other questions with similar issue but none of them helped.
I am trying to read a melspectogram data of audio file.
Sample file i am trying to read
How do i fix this?
with open(file_path, 'rb') as f: instead of with open(file_path, 'r') as f:
or use content.encode() for explicit bytes conversion.
Update
Your question is related to how-load-cv2-keypoint-and-descriptors-correctly-on-opencv-3-with-python-3
Use below code , it should work:
# blues.00000.pp
import pickle, numpy as np
pp_list = np.fromfile('blues.00000.pp')
print('pp_list type :' + str(type(pp_list)))
pp = []
data = dict()
soundID = 'ppFile1'
for i in range(len(pp_list)):
temp = pp_list[i] * 1
pp.append(temp)
pp = np.asarray(pp)
print('pp data : ' + str(pp))
data[soundID] = pp
print('ppdata dic :' + str(data))
Output
pp_list type :<class 'numpy.ndarray'>
pp data : [4.56259939e+257 4.78104776e+180 9.28824150e+242 ... 1.00603813e+165
6.01326204e-067 1.55998657e-259]
ppdata dic :{'ppFile1': array([4.56259939e+257, 4.78104776e+180, 9.28824150e+242, ...,
1.00603813e+165, 6.01326204e-067, 1.55998657e-259])}

Python 3 TypeError: string indices must be integers Error

I'm facing with below error when i'm trying to convert json file to csv.
Could you please help me for understanding and fixing this error ?
reportdata = report_parsed['data']
TypeError: string indices must be integers
.
.
.
with open('report.json', 'w') as outfile:
json.dump(response, outfile)
infile = open('report.json', 'r')
outfile = open('report.csv', 'w')
report_parsed = json.loads(infile.read())
reportdata = report_parsed['data']
csvwriter = csv.writer(outfile)
count = 0
for item in reportdata:
if count == 0:
header = item.keys()
csvwriter.writerow(header)
count += 1
csvwriter.writerow(item.values())
outfile.close()
Sample Json
https://gist.github.com/gokturkenez/0117172b69a30f7be4cc18b3f7a7db7f
report_parsed is a string not a JSON object, Just add another json.loads to your code:
report_parsed = json.loads(json.loads(infile.read()))

Odoo8.0, cannot valid the csv file when try to import it

I have problem when i was try to import .csv file. I was try to convert image to base64 and also i was try to create barcode by name csv file. The image it's success convert into base64 but the problem when i was try to create barcode by csv file name, i was always get error like :
Unknown error during import: <class 'openerp.exceptions.ValidationError'>: ('ValidateError', u'Field(s) `ean13` failed against a constraint: You provided an invalid "EAN13 Barcode" reference. You may use the "Internal Reference" field instead.') at row 2 Resolve other errors first
And this is my code:
files = []
text = ''"
data_text3 = []
header_column2 = ["id","product_variant_ids/ean13_barcode", "product_variant_ids/ean13", "ean13", "image", "ean13_barcode", "default_code", "product_variant_ids/default_code"]
number = 1 for file in os.listdir("gmbr/"):
file_name = os.path.splitext(file)[0]
for n in str(number):
directory_file = "gmbr/"+str(file)
img = open(directory_file, 'rb').read()
img_64 = base64.encodestr
text = str(number)+","+str(name_product)+","+str(file_name)+","+str(file_name)+","+str(img_64+","+" "+","+" "+","+" ")
number += 1
data_text3.append(text)
with open('sample2.csv', 'wb') as f:
writer = csv.writer(f, delimiter='\t', dialect='excel')
writer.writerow(header_column2)
for l in data_text3:
writer.writerow(l.split(','))

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