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

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"]:
^

Related

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

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

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'))

An error about TypeError: expected str, bytes or os.PathLike object, not NoneType

I tried to correct English grammar by running a model.
My development environment is Linux + Anaconda3 + Python 3.6 + CUDA 9.0 + tensorflow1.9.0
After I ran the model, there was the following problem with the test:
Traceback (most recent call last):
File "./generate.py", line 236, in <module>
main(args)
File "./generate.py", line 93, in main
fluency_scorer = FluencyScorer(args.lang_model_path, args.lang_model_data)
File "/home/gpower/zhangtianjiu/NLP/pytorch-human-performance-gec-master/fairseq-scripts/fluency_scorer.py", line 58, in __init__
self.task = tasks.setup_task(self.args)
File "/home/gpower/zhangtianjiu/NLP/pytorch-human-performance-gec-master/fairseq/fairseq/tasks/__init__.py", line 19, in setup_task
return TASK_REGISTRY[args.task].setup_task(args)
File "/home/gpower/zhangtianjiu/NLP/pytorch-human-performance-gec-master/fairseq/fairseq/tasks/language_modeling.py", line 90, in setup_task
dictionary = Dictionary.load(os.path.join(args.data, 'dict.txt'))
File "/home/gpower/anaconda3/envs/tf/lib/python3.6/posixpath.py", line 78, in join
a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not NoneType
How should I solve this problem?
It would be helpful to see some code but it looks like the a variable in a = os.fspath(a) is of type NoneType. The a variable should either be a string, bytes or an os.PathLike object in order to work with the os.fspath function.
You can check the Python documentation on the os.fspath function: python docs

Removing certain strings from base64 output python

Here is my code.
import base64
encoded = base64.b64encode(b"data to be encoded")
print(encoded)
print(encoded.replace("b", ""))
Here is my output
b'ZGF0YSB0byBiZSBlbmNvZGVk'
Traceback (most recent call last):
File "C:\Users\user\Desktop\base64_obfuscation.py", line 8, in <module>
print(decoded.replace("b", ""))
TypeError: a bytes-like object is required, not 'str'
My overall task is to remove the single quotes and the "b" chracter from the string but I'm unsure on how to do so?
print(str(encoded).replace("b", ""))

Python syntax error "cannot concatenate 'str' and 'NoneType' objects"

I found this script on the internet:
https://gist.github.com/gavsmi/dd31746e5847300b62da
Any idea why I am getting the following error message? It looks like a syntax error. I am still new to Python, so please help me point out what the problem is and how to fix it.
[root#ip-172-31-18-97 tmppython]# python snapshot.py
INFO:root:Finding snapshot for tag...
Traceback (most recent call last):
File "snapshot.py", line 164, in <module>
main()
File "snapshot.py", line 30, in main
snapshot = find_snapshot(args.tag_name, args.tag_value)
File "snapshot.py", line 47, in find_snapshot
snapshots = conn.get_all_snapshots(filters={'tag:' + tag_name: tag_value})
TypeError: cannot concatenate 'str' and 'NoneType' objects
You likely are not providing a 'tag_name' command line argument when running your script. Added arguments default to None in the argparse module, so 'args.tag_name' ('tag_name' in the scope where the error occurs) will be None unless you give it a value via the command line. If it is not clear, the “cannot concatenate 'str' and 'NoneType' objects” run-time error results from the attempted concatenation "'tag:' + tag_name", where 'tag_name' is None.

Categories

Resources