I wanted to read a file and extract two lists of doubles for example, file.txt contains the following:
123 345 456 8919
231 521 363 1716
separated by a white space ' '. I have the following code to read the above file:
with open("file.txt") as f:
for line in f.readline():
cipher_array.append(line.split()[0])
cipher_array.append(line.split()[1])
halfmask.append(line.split()[2])
halfmask.append(line.split()[3])
I get the following error:
cipher_array.append(line.split()[1])
IndexError: list index out of range
I want to have cipher_array to consist of [123, 345, 231, 521] and halfmask = [456, 8919, 363, 1716]
What is wrong with my code? Thanks in advance
use split(" ")
instead of split()
also
f.readline()
should be f.readlines()
cipher_array,halfmask=[],[]
with open("file.txt") as f:
for line in f.readlines():
cipher_array.append(float(line.split(" ")[0]))
cipher_array.append(float(line.split(" ")[1]))
halfmask.append(float(line.split(" ")[2]) )
halfmask.append(float(line.split(" ")[3]))
cipher_array,halfmask