Ascii to Binary is getting failed while extracting from mainframe server OS400 - python

We are trying to extract a files from AS400 / OS400 which is in Ascii mode and convert into readable format of binary mode. We have written below code
def retrlines(self, cmd, callback = None):
resp = self.sendcmd('TYPE A')
with self.transfercmd(cmd) as connectioninfo, \
connectioninfo.makefile('rt', encoding="cp500") as fp:
while 1:
line = fp.readline(self.maxline + 1)
callback(line)
return self.voidresp()
Getting an error
TypeError: a bytes-like object is required, not 'str'
Task failed with exception
Traceback (most recent call last):
File "/home/sftp_file_hook.py", line 418, in retrieve_file
conn.retrlines(f'RETR {remote_file_name}', callback)
File "/opt/ftplib.py", line 472, in retrlines
callback(line)
TypeError: a bytes-like object is required, not 'str'
Any suggestion please

Related

How do I deploy Jupyter to Medium? "AttributeError: 'PosixPath' object has no attribute 'read'

I'm trying to deploy Jupyter Notebook to Medium with jupyter_to_medium and still getting the same error. Have tried both Safari and Chrome. I have integration tokens.
MacBook Pro mid 2017
Failed to post to Medium
Returned error message below
"AttributeError: 'PosixPath' object has no attribute 'read'
Traceback (most recent call last):
File \"/usr/local/lib/python3.7/site-packages/jupyter_to_medium/_bundler.py\", line 37, in upload
data = publish(**kwargs)
File \"/usr/local/lib/python3.7/site-packages/jupyter_to_medium/_publish_to_medium.py\", line 299, in publish
chrome_path, save_markdown, table_conversion)
File \"/usr/local/lib/python3.7/site-packages/jupyter_to_medium/_publish_to_medium.py\", line 41, in __init__
self.nb = self.get_notebook()
File \"/usr/local/lib/python3.7/site-packages/jupyter_to_medium/_publish_to_medium.py\", line 76, in get_notebook
return nbformat.read(self.filename, as_version=4)
File \"/usr/local/lib/python3.7/site-packages/nbformat/__init__.py\", line 141, in read
return reads(fp.read(), as_version, **kwargs)
AttributeError: 'PosixPath' object has no attribute 'read'
"
I run into the same error in Python3.7. Here is what worked for me:
The failure cause by this function call reads(fp.read(), as_version, **kwargs) in File \"/usr/local/lib/python3.7/site-packages/nbformat/__init__.py\".
Apparently, fp is a path string, you cannot use read() directly on it.
Changes:
Add the following lines before it:
text = None
with open(fp, 'r') as f:
text = f.read()
and update the original line
return reads(fp.read(), as_version, **kwargs)
to
return reads(text, as_version, **kwargs)

Base64encode bytes-like object is required in python3

My code is:
_cmd = "|| echo " + base64.b64encode(args.cmd) + "|base64 -d|bash"
p.update({"form_284": _cmd})
My error is:
Traceback (most recent call last):
File "openemr_rce.py", line 136, in <module>
_cmd = "|| echo " + base64.b64encode(args.cmd) + "|base64 -d|bash"
File "/usr/lib/python3.8/base64.py", line 58, in b64encode
encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'
Edit:
There is no problem when you run it in python2
Your args.cmd is a string.
If it was meant to be, try base64.b64encode(args.cmd.encode("ascii")).decode("ascii").
If your command contains non-ascii characters, the bash cmd is on a system-dependent encoding, and you can use sys.getdefaultencoding() to fetch it.
As the error says, you must pass a "bytes-like object" to b64encode, not a string. To get bytes from a string, you can call encode():
base64.b64encode(args.cmd.encode('utf8'))

X-13-ARIMA with statsmodels.tsa.x13

I'm trying to use statsmodels.tsa.x13 with my Python 3.6 (anaconda\spider). I`ve already installed x13as and write this code:
X13PATH= os.chdir("C:\\x13\WinX13\\x13as")
x13results = x13_arima_analysis(endog = mb["G"], x12path=X13PATH, outlier=True,print_stdout=True)
where mb["G"] is pandas.core.series.Series. So, the result is following:
C:\Anaconda\lib\site-packages\statsmodels\tsa\x13.py:460: IOWarning: Failed to delete resource C:\Users\SERGEY~1\AppData\Local\Temp\tmp2iwvb0uo.spc
IOWarning)
C:\Anaconda\lib\site-packages\statsmodels\tsa\x13.py:463: IOWarning: Failed to delete resource C:\Users\SERGEY~1\AppData\Local\Temp\tmp_h3vwxc9
IOWarning)
Traceback (most recent call last):
File "<ipython-input-3-8e98768a4534>", line 2, in <module>
x13results = x13_arima_analysis(endog = mb["G"], x12path=X13PATH, outlier=True,print_stdout=True)
File "C:\Anaconda\lib\site-packages\statsmodels\tsa\x13.py", line 434, in x13_arima_analysis
ftempin.write(spec)
File "C:\Anaconda\lib\tempfile.py", line 483, in func_wrapper
return func(*args, **kwargs)
TypeError: a bytes-like object is required, not 'str'
What's the problem? I will be grateful for any help.
You need to pass as a string. Change
X13PATH= os.chdir("C:\\x13\WinX13\\x13as")
to
X13PATH= "C:\\x13\WinX13\\x13as"
From the statsmodels docs: "x12path (str or None) – The path to x12 or x13 binary. If None, the program will attempt to find x13as or x12a on the PATH or by looking at X13PATH or X12PATH depending on the value of prefer_x13."

"TypeError: object not callable" while using tokenize.detect_encoding()

I'm reading a bunch of txt.gz files but they have different encoding (at least UTF-8 and cp1252, they are old dirty files). I try to detect the encoding of fIn before reading it in text-mode but I get the error: TypeError: 'GzipFile' object is not callable
The corresponding code:
# detect encoding
with gzip.open(fIn,'rb') as file:
fInEncoding = tokenize.detect_encoding(file) #this doesn't works
print(fInEncoding)
for line in gzip.open(fIn,'rt', encoding=fInEncoding[0], errors="surrogateescape"):
if line.find("From ") == 0:
if lineNum != 0:
out.write("\n")
lineNum +=1
line = line.replace(" at ", "#")
out.write(line)
Traceback
$ ./mailmanToMBox.py list-cryptography.metzdowd.com
('Converting ', '2015-May.txt.gz', ' to mbox format')
Traceback (most recent call last):
File "./mailmanToMBox.py", line 65, in <module>
main()
File "./mailmanToMBox.py", line 27, in main
if not makeMBox(inFile,outFile):
File "./mailmanToMBox.py", line 48, in makeMBox
fInEncoding = tokenize.detect_encoding(file.readline()) #this doesn't works
File "/Users/simon/anaconda3/lib/python3.6/tokenize.py", line 423, in detect_encoding
first = read_or_stop()
File "/Users/simon/anaconda3/lib/python3.6/tokenize.py", line 381, in read_or_stop
return readline()
TypeError: 'bytes' object is not callable
EDIT
I tried to use the following code:
# detect encoding
readsource = gzip.open(fIn,'rb').__next__
fInEncoding = tokenize.detect_encoding(readsource)
print(fInEncoding)
I have no error but it always return utf-8 even when it isn't. My text editor (sublime) detect correctly the cp1252 encoding.
As the documentation of detect_encoding() says, it's input parameter has to be a callable that provides lines of input. That's why you get a TypeError: 'GzipFile' object is not callable.
import tokenize
with open(fIn, 'rb') as f:
codec = tokenize.detect_encoding(f.readline)[0]
... codec will be "utf-8" or something like that.

python:"\" in str a bytes-like object is required, not 'str'

if "\\" in item["message"]:
item["message"] = str(item["message"]).replace("\\", "\\\\").encode("utf-8")
write the program, there are many "\" in item['message'], therefore, when trying to insert this item in mysql table, it errors, I try to deal with the "\" in item["message"], so I write the program above,
when it run,it errors:
Traceback (most recent call last):
File "C:/Python/PyCharmProject/FaceBookCrawl/FBCrawl.py", line 237, in <module>
group_download.group_feed_storage(FB_access_token,group_id,group_name)
File "C:\Python\PyCharmProject\FaceBookCrawl\group_download.py", line 116, in group_feed_storage
mysql_manage().group_feed_db_manage(type,group,name,feed)
File "C:\Python\PyCharmProject\FaceBookCrawl\database_manage.py", line 95, in group_feed_db_manage
if "\\" in item["message"]:
TypeError: a bytes-like object is required, not 'str'
Process finished with exit code 1
Could you please help me for that
item['message'] is a bytes-like object, so it may contain certain sequences of bytes, but won't contain substrings. Search for a bytes-like object:
if b"\\" in item["message"]:
^

Categories

Resources