简体   繁体   English

使用Python在三个不同的站中读取三组数据

[英]Using Python to read three sets of data in three different stations

I have a file like this (data.txt): 我有一个像这样的文件(data.txt):

Each column contains data from 3 different stations (1,2,3..1,2,3..so on), so now I want to read the data from each station 1(line 1, line 4, line 7), station 2 (line 2, line 5, line 8) and station 3 (line 3, line 6, line 9).... 每列包含来自3个不同站点(1,2,3..1,2,3..so等)的数据,所以现在我想从每个站点1(第1行,第4行,第7行)读取数据,车站2(2号线,5号线,8号线)和车站3(3号线,6号线,9号线)....

How could I do? 我该怎么办? My appologies if someone had asked before. 如果有人问过我,我很高兴。

Thanks 谢谢

If your data is comma delimited this will work: 如果您的数据以逗号分隔,则可以使用:

with open('data.txt') as d:
    data = [list(map(float, line.split(','))) for line in d]
    station1 = data[::3] #from first with step 3
    station2 = data[1::3] #from second with step 3
    station3 = data[2::3] #from third with step 3
    print station1
    print station2
    print station3

However, if you use other delimiter change split() argument to it's value. 但是,如果使用其他定界符,请将split()参数更改为其值。

EDIT 编辑

You must be using Python 3.x so you need to convert map into list. 您必须使用Python 3.x因此需要将map转换为list。 In 3.x map() returns iterator. 3.x map()返回迭代器。

with open("myfile.txt") as f:
    lines = [line.rstrip('\n').split(" ") for line in f]


station_1_data = lines[0::3]

station_2_data = lines[1::3]

station_3_data = lines[2::3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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