将字符串拆分为数字(Python)

将字符串拆分为数字(Python),python,string,list,floating-point,Python,String,List,Floating Point,这可能是一个非常基本的问题。我有一个文本文件,其中有几行浮点值。例如,文本文件myRandomizedFile.txt如下所示: 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.

这可能是一个非常基本的问题。我有一个文本文件,其中有几行浮点值。例如,文本文件
myRandomizedFile.txt
如下所示:

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
然而,当我试图通过索引访问这些数字时,我得到的只是每个字符串字符。例如,如果我想通过尝试使用
allTen[0][0]
的索引来访问左上角的数字
1992.0
,它告诉我
allTen[0]=1

下面是我的代码:

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]
最底层的一行是给我一个
索引器
,因为
allTen[0]
1
,而不是
[1992.0 12.999 0.0 0 0.0 7980]
如何让程序将其识别为浮点列表而不是一堆字符(字符串)?

给您:

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])
输出

1992.0
给你:

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])
输出

1992.0

通常需要读取数组格式的文件,去掉新行字符并拆分列表,然后得到解决方案

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

通常需要读取数组格式的文件,去掉新行字符并拆分列表,然后得到解决方案

    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']   
您可以将NumPy与一起使用。下面是一个演示:

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)
结果:

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]]
您可以将NumPy与一起使用。下面是一个演示:

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)
结果:

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

您必须先拆分字符串。尝试<代码> ALTEN.SPLIT(0)< /Cord> >您可以考虑<代码>熊猫> /COD>:<代码> Pd。Read Oracle CSV(MyReavixDetry.txt),Health=无,DelimeWaleStase= true)< /C> >您必须先写代码> String < /Cord>字符串。尝试<代码> ALTEN.SPLITE()/代码>您可以考虑<代码>熊猫> <代码>:代码> Pd .Read Oracle CSV(MyRealixDebug文件.txt),页眉=没有,DelimeWaleStase= TRUE)< /Cord> >如果您要使用NUMPY,我认为最好避免首先创建列表,例如使用代码> NP.GEnFROMTMTX。我只是列表理解的大粉丝;不反对在列表理解中使用NUMPYNOTH错误。但是,在这种情况下,您不需要
np.array
。我需要它来
重塑
。可能有几种解决方案,但如果你打算使用NumPy,我认为最好先避免创建列表,例如使用
np.genfromtxt
。我只是列表理解的忠实粉丝;)不反对在列表理解中使用NUMPYNOTH错误。但是,在这种情况下,您不需要
np.array
。我需要它来
重塑
。不过,可能有几种解决方案