-3

I have a textfile .txt with the following structure:

a-011bbbad022b7889c
a-0cf9f08ade83aed64
a-0408c0e6745bc2563
a-074b8709a7e4c9751
a-0d083d670fd0587a9
a-0670147fdd4dfec3a
a-075429f00fe0cf19a
a-0b734cd1f480a2f3c
a-0d1422812bc7344f1
a-092b72359be9b447b

What is the easiest way to get data into an array?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

3 Answers3

0
import struct
output_arr = []
with open('somefile.txt', 'r') as f:
    for line in f:
        float_num = struct.unpack('!f', bytes.fromhex(line.split('a-')[1]))[0]
        output_arr.append(float_num)
BTables
  • 4,413
  • 2
  • 11
  • 30
0

As stated in the comments, you can do this in just one line:

result = open("test.txt").read().splitlines()
Paolopast
  • 197
  • 1
  • 11
0

Open and read


A .txt file can be opened and read like this:

with open(r"C:\\Path\to\file.txt", 'r') as file:
    content = file.read()

Split


Then, you can get the lines like this:

content = content.splitlines()
FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28