How to convert binary string to binary python - python

I have a problem and I searched a lot and I didn't found an answer.
I read from file as example : "video.mp4" by binary
I get as an example : b'\x00\x02\x1a\x00' (binary list)
I saved it as string in file : b'\x00\x02\x1a\x00' (string)
I read it again as string : "b'\x00\x02\x1a\x00" (string)
I want to convert it again to binary, but no result !!
sometimes I get it like this b"b'\x00\x02\x1a\x00"'
Any answer ??
Oh sorry, here i made simple code because the original is messed up
#!/usr/bin/python
FILE = open("video.mp4", "rb")
FILE2 = open("video", "w")
chunk = FILE.read(8)
FILE2.write(str(chunk))
FILE.close()
FILE2.close()
FILE = open("video", "r")
line = FILE.readline()
print(line)
print(line == str(chunk))
FILE.close()
FILE = open("video_binary", "wb")
FILE.write(line) # Here I want line to convert to binary
FILE.close()
But it same thing

Related

Slice a given txtfile and write only part of it in a newfile in python

This is my original .txt data:
HKEY_CURRENT_USER\SOFTWARE\7-Zip
HKEY_CURRENT_USER\SOFTWARE\AppDataLow
HKEY_CURRENT_USER\SOFTWARE\Chromium
HKEY_CURRENT_USER\SOFTWARE\Clients
HKEY_CURRENT_USER\SOFTWARE\CodeBlocks
HKEY_CURRENT_USER\SOFTWARE\Discord
HKEY_CURRENT_USER\SOFTWARE\Dropbox
HKEY_CURRENT_USER\SOFTWARE\DropboxUpdate
HKEY_CURRENT_USER\SOFTWARE\ej-technologies
HKEY_CURRENT_USER\SOFTWARE\Evernote
HKEY_CURRENT_USER\SOFTWARE\GNU
And I need to have a new file where the new lines contain only part of those strings, like:
7-Zip
AppDataLow
Chromium
Clients
...
how to do it in python?
Try this:
## read file content as string
with open("file.txt", "r") as file:
string = file.read()
## convert each line to list
lines = string.split("\n")
## write only last part after "\" in each line
with open("new.txt", "w") as file:
for line in lines:
file.write(line.split("\\")[-1] + "\n")
One approach would be to read the entire text file into a Python string. Then use split on each line to find the final path component.
with open('file.txt', 'r') as file:
data = file.read()
lines = re.split(r'\r?\n', data)
output = [x.split("\\")[-1] for x in lines]
# write to file if desired
text = '\n'.join(output)
f_out = open('output.txt', 'w')
f_out.write(text)
f_out.close()

trying to search a key word and if exists replacing the whole line : getting error :io.UnsupportedOperation: not readable

trying to replace whole line if it matches with specefic character but getting an error below as io.UnsupportedOperation: not readable
trying to replace if "date=datetime(" exists then replace the complete line (in the file) with something : date=datetime(2020, 1, 1)
pls anyone suggest.
Sampledata: /home/arya/config.txt
/home/file.txt,date=datetime(2020, 1, 1)
/home/file2.txt,date=datetime(2020, 1, 20)
for variable in open("/home/arya/config.txt","r"):
filename=variable.rstrip('\n').split(',')[0]
replaceval=variable.rstrip('\n').split(',')[1]
outFile = open(filename,'w')
for line in outFile:
if "date=datetime(" in line:
line=replaceval
outFile.write(line)
Error : io.UnsupportedOperation: not readable
content of /home/file.txt
import os
date=datetime(2099, 1, 1) ## this value may change in each file
trying to get something like below after replace
import os
date=datetime(2020, 1, 1)
outfile is being opened with the 'w' flag, which means a new file is opened and there's nothing to iterate over.
for variable in open("/home/arya/config.txt", "r"):
filename = variable.rstrip("\n").split(",")[0]
replaceval = variable.rstrip("\n").split(",")[1]
originalContents = open(filename, "r").readlines()
outfile = open(filename, "w")
for line in originalContents:
if "date=datetime(" in line:
line = replaceval
outFile.write(line)
This will set the original contents to the variable originalContents using the readlines() method, this returns a list of all lines in the original file.
Then the same filename is opened with the 'w' flag, which overwrites it. The new contents are then written to it. The contents of line are modified as needed.

Can you write to the middle of a file in python?

I would like to write to the middle of a line in a file.
for exemple i have a file:
Text.txt:
"i would like to insert information over here >>>>>>>[]<<<<<<<<"
Is is it possible to precise an index where: file.write() has to start writing?
I have started with this:
file = open(file_path, 'w')
file.write()
I think what you can do is to substitute already existing characters with the same amount of other characters you want. You can open a file, locate the starting point, and start writing. But you will overwrite all the following bytes if you use f.write(). If you want to "insert" something inbetween, you have to read and rewrite all the following content of the file.
Overwrite:
with open('text.txt', 'w') as f:
f.write("0123456789")
# now the file 'text.txt' has "0123456789"
with open('text.txt', 'r+b') as f:
f.seek(-4, 2)
f.write(b'a')
# now the file 'text.txt' has "012345a789"
Insert:
with open('text.txt', 'w') as f:
f.write("0123456789")
# now the file 'text.txt' has "0123456789"
with open('text.txt', 'r+b') as f:
f.seek(-4, 2)
the_rest = f.read()
f.seek(-4, 2)
f.write(b'a')
f.write(the_rest)
# now the file 'text.txt' has "012345a6789"
import fileinput
file = [The file where the code is]
for line in fileinput.FileInput(file, inplace=1):
if [The text that should be in that line] in line:
line = line.rstrip()
line = line.replace(line, [The text that should be there after this file was run])
print (line,end="")
As text in that line you should enter the whole line, else it could not work (I didn't test it out though)

Python read .txt and split words after symbol #

I have a large 11 GB .txt file with email addresses. I would like to save only the strings till the # symbol among each other. My output only generate the first line.I have used this code of a earlier project. I would like to save the output in a different .txt file. I hope someone could help me out.
my code:
import re
def get_html_string(file,start_string,end_string):
answer="nothing"
with open(file, 'rb') as open_file:
for line in open_file:
line = line.rstrip()
if re.search(start_string, line) :
answer=line
break
start=answer.find(start_string)+len(start_string)
end=answer.find(end_string)
#print(start,end,answer)
return answer[start:end]
beginstr=''
end='#'
file='test.txt'
readstring=str(get_html_string(file,beginstr,end))
print readstring
Your file is quite big (11G) so you shouldn't keep all those strings in memory. Instead, process the file line by line and write the result before reading next line.
This should be efficient :
with open('test.txt', 'r') as input_file:
with open('result.txt', 'w') as output_file:
for line in input_file:
prefix = line.split('#')[0]
output_file.write(prefix + '\n')
If your file looks like this example:
user#google.com
user2#jshds.com
Useruser#jsnl.com
You can use this:
def get_email_name(file_name):
with open(file_name) as file:
lines = file.readlines()
result = list()
for line in lines:
result.append(line.split('#')[0])
return result
get_email_name('emails.txt')
Out:
['user', 'user2', 'Useruser']

Converting the file of decimal values into hex format

I am new to python and I am trying to write a simple script that will convert each row of my .txt file containing decimals into hex format and will save it into another .txt file. My input has the 16 bit values in decimal format such as
15166
46818
26814
640
44756
27831
2646
This is a snippet that I have so far:
import binascii
filename = '1.txt'
content = f.read()
out = binascii.hexlify(content)
f = open('out.txt', 'wb')
f.write(out)
f.close()
This is the output that I am getting 31353136360d0a34363831380d0a32363831340d0a3634300d0a34343735360d0a32373833310d0a323634360d0a393237360d0a323238390d0a333330320d0a33393137370d0a393535340d0a363239310d0a31353438310d0a33353632300d0a35373330310d0a33323933350d0a3834380d0a34313639330d0a33353538340d0a31363936390d0a31313539300d0a31343639350d0a36333931350d0a393238340d0a33323339370d0a343235330d0a33323934320d0a31303139340d0a34393238360d0a34383430370d0a31333330350d0a3336340d0a36323735340d0a32313438310d0a35323734350d0a31303931310d0a34323835380d0a373731370d0a34393530320d0a35313034380d0a36323832330d0a34343833370d0a36313934300d0a33393137310d0a33333032320d0a32333836360d0a36313335360d0a31393038380d0a35393135340d0a36353335320d0a32343233300d0a32303936310d0a34313134330d0a35343433350d0a36343038380d0a35323334340d0a33373136370d0a32363734390d0a36353439300d0a36353236360d0a36313234320d0a33343933360d0a313532360d0a35313236310d0a33353039350d0a36303931350d0a34313336350d0a32333235370d0a333133350d0a33373433380d0a34363837350d0a363831390d0a34373034320d0a31373035380d0a363734350d0a35313135340d0a333535330d0a33343134320d0a36353334360d0a34343334310d0a35333330370d0a35333232320d0a34313336300d0a33383037300d0a32363134350d0a34343532310d0a34373836360d0a34393033360d0a36323037320d0a34373630330d0a34363337300d0a34303534360d0a31393231330d0a373930340d0a393839340d0a31383337350d0a35383231360d0a33353033380d0a31333338310d0a32313637350d0a33383333370d0a35393430340d0a31333933300d0a31353830370d0a33373434370d0a31313832370d0a34383331360d0a32393433350d0a32363831360d0a36313035360d0a34303533350d0a33383335340d0a31373037370d0a34383236360d0a31363237350d0a34343331370d0a35343836320d0a34303730370d0a32363735370d0a32353438380d0a3737320d0a32363038330d0a32373339370d0a35323934380d0a34313537340d0a32363934310d0a3433353539
So I need that each entry will be separated and displayed as a list in my output file. If I do have the line
for c in out:
print(c)
I get the huge list with two decimals in each of it and seemed to be wrong. Please post any solution for this problem.
Another way to do it is using hex like so:
filename = '1.txt'
newfile = '2.txt'
with open(filename, 'r') as f:
numbers = f.read().splitlines()
with open(newfile, 'w') as n:
for num in numbers:
n.write('{}\n'.format(hex(int(num))))

Categories