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", ""))
Related
I have a list.txt where every line has a different hex string. So I want to choose a line and convert the specific line in ascii. So I have written this code:
import codecs
f=open('list.txt')
lines=f.readlines()
var=lines[25]
print(var)
print( codecs.decode("{var}", "hex") )
The specific var = 2d560d4b0618203249312a310d5f541f295c3f0f25235c2b20037d1600f3
when I execute this command:
print( codecs.decode("2d560d4b0618203249312a310d5f541f295c3f0f25235c2b20037d1600f3", "hex") ) I get the result.
But when I try to put the var variable I get this error:
Traceback (most recent call last): File "/usr/local/lib/python3.7/encodings/hex_codec.py", line 19, in ee4'hex_decode return (binascii.a2b_hex(input), len(input))binascii.Error: Odd-length string
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "main.py", line 8, in <module>
print( codecs.decode("{var}", "hex") )
binascii.Error: decoding with 'hex' codec failed (Error: Odd-length string) `
The new lines were being included in the string. Here is a working version that strips the white-space characters.
import codecs
f=open('list.txt')
lines=f.readlines()
var=lines[25].strip()
print(codecs.decode(var, "hex"))
Example code:https://onlinegdb.com/S1ZVk4jiH
I often use print() instead of file.write() when writing to files in Python. Recently, I had to write out some binary data, and tried a similar pattern:
f = open('test.txt', 'w')
f.write('abcd')
print('efgh', file=f)
f.close()
f = open('test.bin', 'wb')
f.write(b'abcd') # works as expected
print(b'efgh', file=f)
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: a bytes-like object is required, not 'str'
f.close()
My first thought was the default newline-appending behaivor of print() might be at fault:
print(b'efgh', file=f, end='')
# same error
print(b'efgh', file=f, end=None)
# same error
print(b'efgh', file=f, end=b'')
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: end must be None or a string, not bytes
print(b'efgh', file=f, end=b'\n')
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: end must be None or a string, not bytes
Unfortunately, none of the permutations of changing the end worked.
Why is this happening? I suspect that this use of print() might not be supported, but in that case the error message is quite confusing.
From python3 documentation: https://docs.python.org/3/library/functions.html#print
All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.
As I understand, you can't write bytes with print() because the object you want to write is passed only as positional argument and positional arguments are converted to strings(hence the error).
I want to use the function base64.encode() to directly encode the contents of a file in Python.
The documentation states:
base64.encode(input, output)
Encode the contents of the binary input file and write the resulting base64 encoded data to the output file. input and output must be file objects.
So I do this:
encode(open('existing_file.dat','r'), open('output.dat','w'))
and get the following error:
>>> import base64
>>> base64.encode(open('existing_file.dat','r'), open('output.dat','w'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/base64.py", line 502, in encode
line = binascii.b2a_base64(s)
TypeError: a bytes-like object is required, not 'str'
To my eye that looks like a bug in /usr/lib/python3.6/base64.py but a big part of me does not want to believe that...
from docs
when opening a binary file, you should append 'b' to the mode value to open the file in binary mode
so changing
encode(open('existing_file.dat','r'), open('output.dat','w'))
to
encode(open('existing_file.dat','rb'), open('output.dat','wb'))
should work
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"]:
^
I have a file encoded in hex and I'm trying to decode the file however I keep getting a type error. I have only been using python on and off for a few weeks so if this seems like a basic question I apologize.
The file contents is as follows:
4647525137454353554e54544b5831375a42524d345742473246563639554e4a36495a3359304f35394843554637564d4d464f32354143574f495a4f4a4a565849373259544f46335a4358494b424e335047545a51534b47465259475956584d44594f473536494553373653455932574b33574431435a314d35545957594d4e57434444344948324d375858544f4c564f31444a45304947394c32375a584f4845535a534f43353859594c55594e4239363759393738313557475859345a474448434e4f5a5744544d696c6c656e69756d323030303a3035303233626566343737386639343461626439346334653364623062326166
here is the code I ran:
"received_files/documents/cache/OCAGS0WFYO57JVFGUI4Z437.txt".decode("hex")
This is what I got back:
Traceback (most recent call last):
File "converter.py", line 1, in <module>
"received_files/documents/cache/OCAGS0WFYO57JVFGUI4Z437.txt".decode("hex")
File "/usr/lib/python2.7/encodings/hex_codec.py", line 42, in hex_decode
output = binascii.a2b_hex(input)
TypeError: Non-hexadecimal digit found
You're giving it a filename rather than the contents of that file:
"received_files/documents/cache/OCAGS0WFYO57JVFGUI4Z437.txt".decode("hex")
Try this:
open("received_files/documents/cache/OCAGS0WFYO57JVFGUI4Z437.txt").read().decode("hex")