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))))
Related
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
I am trying to convert a list of strings to byte64 format so I can decode them and download them as Images. The byte64 format images are stored in file.txt and a snippet of the file is shown below. My attempt only does this for a single byte64 format string, how should I do this for a file containing multiple lines?
snippet from file.txt:
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/...
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/...
Here's what I've tried to do:
# read lines from file
f = open("file.txt", "r")
for x in f:
data = f.readlines()
f.close()
# Removing unecessary substrings
images = [w.replace('\n', '') for w in data]
images = [w.replace('data:image/jpeg;base64,', '') for w in images]
# encode string to byte64 format
test = images[1].encode()
# Convert to image
with open("image_1.png", "wb") as fh:
fh.write(base64.decodebytes(test))
If all of your code is working, then it should be as simple as changing the index and file name.
data = []
with open("file.txt", "r") as f
# for x in f: i dont know why this was in a loop
data = f.readlines()
# do this in one line
images = [w.replace('\n', '').replace('data:image/jpeg;base64,', '') for w in data]
for index, base64string in enumerate(images):
test = base64string.encode()
with open("image_{0}.png".format(index), "wb") as fh:
fh.write(base64.decodebytes(test))
I haven't run this code, but it looks like it should work to me.
I am reading hex data from a .csv file that has multiple rows (example format: FFFDF3FFFBF2FFFAF210FFF0) using the following code:
with open('c:\\temp\\results.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=",")
line_count = 0
file = open('c:\\temp\\sent.csv', 'w')
for row in csv_reader:
hex_string = f'{row[0]}'
bytes_object = bytes.fromhex(hex_string)
file.write(str(bytes_object) + '\n')
line_count += 1
file.close()
The output file contains mutliple rows that are converted to this format (sorry new to python so not sure if this is a bytearray or what it is actually called): b'\xff\xfd\xf3\xff\xfb\xf2\xff\xfa\xf2\x10\xff\xf0'
I am trying to convert back from this format to the orginal format reading the rows of the newly created .csv file (need to edit readable ascii in the file and covert back for use in another program).
file = open('c:\\temp\\sent.csv', 'r')
for row in file:
byte_string = row
# hex_object = byte_string.hex()
#THIS works if I enter the byte array in directly, but not if reading
#from file hex_object = byte_string.hex()
hex_object = b'\xff\xfd\x03\xff\xfb\x03\xff\xfd\x01\xff\xfb\x17\xff\xfa\xff\xf0\xff\xfd\x00\xff\xfb\x00'.hex()
print(hex_object)
#print(byte_string)
# writer.writerow(hex_object)
Is there a way to get this to work? I have tried several encoding methods, but since the data is already in the proper format I really just need to get it in a readable type for the .hex() method. I am using the latest version of Python 3.8.1enter code here
You are storing a textual representation of your bytes object and then trying to read it back without conversion to/from binary. Instead you are better off opening the output file in binary format like this:
file = open('c:\\temp\\sent.csv', 'wb')
and write the bytes to file:
bytes_object = bytes.fromhex(hex_string)
file.write(bytes_object)
(no need for newline character).
Then to do the opposite open in binary format:
with open('c:\\temp\\sent.csv', "rb") as f:
data = f.read()
s = data.hex()
print(s)
Here data is a bytes object and it has the hex() function you are looking for.
I have a text file with about 2000 numbers, they are written to the file in a random order...how can i order them from within python? Any help is appreciated
file = open('file.txt', 'w', newline='')
s = (f'{item["Num"]}')
file.write(s + '\n')
file.close()
read = open('file.txt', 'a')
sorted(read)
You need to:
read the contents of the file: open('file.txt', 'r').read().
split the content using a separator: separator.split(contents)
convert each item to a number, otherwise, you won't be able to sort numerically: int(item)
sort the numbers: sorted(list_of_numbers)
Here is a code example, assuming the file is space separated and that the numbers are integers:
import re
file_contents = open("file.txt", "r").read() # read the contents
separator = re.compile(r'\s+', re.MULTILINE) # create a regex separator
numbers = []
for i in separator.split(f): # use the separator
try:
numbers.append(int(i)) # convert to integers and append
except ValueError: # if the item is not an integer, continue
pass
sorted_numbers = sorted(numbers)
You can now append the sorted content to another file:
with open("toappend.txt", "a") as appendable:
appendable.write(" ".join(sorted_numbers)
I have a text file that consists of 1 column of numbers (like: 1e-5, 1.15e3, etc).
How do I save the column to a list as just floats?
input text file looks like this:
1.00E-05
1.06E-05
1.13E-05
1.19E-05
I tried this:
file=open('C:\Users\Enrique Jr\Desktop\h_energy.txt', "r")
h_energy=[]
for line in file:
h_energy.append((line.split()))
file.close()
and got an output of this:
[['1.00E-05'], ['1.06E-05'], ['1.13E-05'], ['1.19E-05']
You can use the csv reader library in python: https://docs.python.org/2/library/csv.html
It will be something like this:
>>> import csv
>>> with open('output.txt', 'r') as csvfile:
... data = csv.reader(csvfile, delimiter='\t')
... print data
[["1", "2"], ["3.4", "5"], ...]
You will need to convert the data from text to float or int yourself.
try this:
file=open('output.txt', "r")
array = []
for line in file:
array.append(float(line.rstrip()))
file.close()
file=open('output.txt', "r")
for line in file:
list.append(float(line.split("\t")[0]))
list.append(float(line.split("\t")[1]))
file.close()
If you require ints and floats to be stored as different types and not just have
float representation of integers, then you would need to try-catch to see if you can
parse as an int, and if not then as a float:
try:
list.append(int(line.split("\t")[0]))
except ValueError:
try:
inNumberfloat = float(inNumber)
list.append(float(line.split("\t")[0]))
except ValueError:
#neither an int nor float