从文件中制作空白分隔数字列表

问题描述 投票:0回答:1

我想从文件中读取一些数字,这些数字是我无法在两个列表中读取的,用于进一步计算,如Mean和STDEV。 0.0000000 0.0000005 0.0100000 0.1675796 0.0200000 0.2042502 0.0300000 0.2064999 0.0400000 0.2237432 0.0500000 0.2245723 0.0600000 0.2365732 0.0700000 0.2433299 0.0800000 0.2556339 0.0900000 0.2569953 0.1000000 0.2658122 0.1100000 0.2718526 0.1200000 0.2741648

import os 

directory= "/media/quinn/Joker/post_analysis/KBH00"

for x in range (252, 256):
    os.chdir(directory + str(x) +'/')
    print(os.getcwd())
    with open ('rmsd.xvg', 'r') as rmsd:
      line_19_to_end = rmsd.readlines()[18:]
      print("Values of RMSD are:")
      for line in line_19_to_end:
        print("%s" %(line))
python python-3.x file-handling
1个回答
0
投票

在迭代时,将每个值添加到浮点后,将第0个位置的所有值添加到新列表col1,将第一个位置添加到另一个列表col2

import os 

directory= "/media/quinn/Joker/post_analysis/KBH00"

for x in range (252, 256):
    os.chdir(directory + str(x) +'/')
    print(os.getcwd())
    col1,col2=[],[]
    with open ('rmsd.xvg', 'r') as rmsd:
      line_19_to_end = rmsd.readlines()[18:]
      print("Values of RMSD are:")
      for line in line_19_to_end:
        print("%s" %(line))
        line = list(map(float, line.split()))
        col1.append(line[0])
        col2.append(line[1])
print(col1,'\n',col2)
© www.soinside.com 2019 - 2024. All rights reserved.