简体   繁体   中英

Splitting strings into numbers (Python)

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

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

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]]

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']   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM