This is likely a very basic question. I have a text file with lines of float values. For example, the text file myRandomizedFile.txt looks like this:
1992.0 12.999 0.0 0.0 7980.0
1991.0 11.593 0.625 0.0 7997.0
1992.0 12.999 0.625 0.0 7989.0
1992.0 12.999 0.375 0.0 7998.0
1994.0 14.989 0.0 0.0 7982.0
110.0 42.945 1.0 0.0 7973.0
1992.0 15.077 0.125 0.0 7973.0
492.0 8.824 0.25 1.0 7980.0
1991.0 20.401 0.0 0.0 7997.0
1993.0 12.999 0.625 0.0 7934.0
However, when I try to access these numbers through indices, all I get is each string character. For example, if I want to access the top left number, 1992.0, by trying to use index of allTen[0][0], it tells me that allTen[0] = 1.
Below is my code:
f = open("../BestTen.txt") #Randomize the parameter set order for pairing
o = open("../myRandomizedFile.txt", "w")
entire_file = f.read()
lines_in_list = entire_file.split("\n")
num_lines = len(lines_in_list)
random_nums = random.sample(xrange(num_lines), num_lines)
for i in random_nums:
o.write(lines_in_list[i] + "\n")
o.close()
f.close()
rand = open("../myRandomizedFile.txt") #Pairs up lines (1,2), (3,4), (5,6), (7,8), (9,10)
allTen = rand.read()
print "AllTen: ", allTen
print "AllTen[0]: ", allTen[0]
ind1Aff = allTen[0][0]
ind2Aff = allTen[1][0]
ind1Vff = allTen[0][1]
The bottom-most line is giving me an IndexError because allTen[0] is 1 instead of [1992.0 12.999 0.0 0.0 7980]. How do I get the program to recognize this as a list of floats rather than a bunch of characters (strings)?
splitthe string first. TryallTen.split()[0]pandas:pd.read_csv('myRandomizedFile.txt', header=None, delim_whitespace=True)