0

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)?

2
  • 1
    You have to split the string first. Try allTen.split()[0] Commented Jul 31, 2018 at 14:02
  • You might consider pandas: pd.read_csv('myRandomizedFile.txt', header=None, delim_whitespace=True) Commented Jul 31, 2018 at 14:04

3 Answers 3

2

Here you go:

with open("myRandomizedFile.txt") as file:
    lines = file.readlines()
    allTen = np.array([float(i) for l in lines for i in l.split()]).reshape((len(lines), 5))

print (allTen[0][0])

Output

1992.0
Sign up to request clarification or add additional context in comments.

4 Comments

If you're going to use NumPy, I think it's better to avoid creating a list first, e.g. use np.genfromtxt.
I am just a big fan of list comprehensions ;) Nothing against using NumPy
Nothing wrong with a list comprehension. But, in that case, you don't need np.array.
I needed it for reshape. There might be several solutions to get around it though
1

You can use NumPy with np.genfromtxt. Here's a demo:

from io import BytesIO

x = BytesIO(b"""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""")

res = np.genfromtxt(x)

Result:

print(res)

[[  1.99200000e+03   1.29990000e+01   0.00000000e+00   0.00000000e+00
    7.98000000e+03]
 [  1.99100000e+03   1.15930000e+01   6.25000000e-01   0.00000000e+00
    7.99700000e+03]
 ...
 [  1.99100000e+03   2.04010000e+01   0.00000000e+00   0.00000000e+00
    7.99700000e+03]
 [  1.99300000e+03   1.29990000e+01   6.25000000e-01   0.00000000e+00
    7.93400000e+03]]

Comments

0

You normally have to read files in array format and strip the new line character and split the list, then you got you solution.

    with open('myRandomizedFile.txt', 'r') as f:
        data = f.readlines()
        data = [l.strip().split() for l in data]
        print(data[0][0])
     #output as: data[0][0]: 1992.0
     #            data[1]: ['1991.0', '11.593', '0.625', '0.0', '7997.0']   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.