【发布时间】:2016-09-28 15:59:46
【问题描述】:
您好,我有一个问题。我有以下文本文件(3 列和之间的空格)。我想将每一列(tt,ff,ll)插入到它自己的列表(时间,l,f)中。
文本文件:
0.0000000000000000 656.5342434532456082 0.9992059165961109
1.0001828508431749 656.5342334512754405 1.0009810769697651
2.0003657016863499 656.5342259805754566 0.9989386155502871
3.0005485525295246 656.5342339081594218 1.0005032672779635
4.0007314033726997 656.5342356101768928 0.9996101946453564
5.0009142542158749 656.5342236489159404 0.9986884414684027
6.0010971050590491 656.5342474828242985 1.0001061182847479
7.0012799559022243 656.5342355894648563 1.0003982380731031
8.0014628067453994 656.5342256832242356 0.9993176599964499
9.0016456575885737 656.5342218575017341 0.9999117456245585
10.0018285084317498 656.5342408970133192 1.0000973751521087
11.0020113592749240 656.5342243211601954 0.9997189612768125
12.0021942101180983 656.5342320396634932 0.9997487346699927
13.0023770609612743 656.5342291293554808 0.9991986731183715
但我想要以下输出:
time: (0.00,1.00,2.003,4.0007 etc...)
l: (656.53,656.53,656.53,656.53 etc..)
f: (...)
尝试代码:
from numpy import *
def read_file(filename):
time = [] # list time
f = [] # ...
l = [] # ...
infile = open(filename, "r") # reads file
for line in infile: # each line in txt file
numbers = line.split() # removes the " "
tt = numbers[0] # 1st column?
ff = numbers[1] # 2nd column?
ll = numbers[2] # 3rd column?
time.append(tt) # Inserts 1st column(tt) into list(time)
f.append(ff) # ...
l.append(ll) # ...
return time,f,l # return lists
txt1 =read_file("1.txt") # calls function
print txt1 # print return values
【问题讨论】:
-
我运行了代码并且工作正常。您面临的具体问题是什么?
标签: python list text-files multiple-columns