我有一个数据文本文件,其格式如下:
1 2 2 3 4 5 6
1 5 8 9 3 4 2
1 2 3 5 1 2 3
Timestamp 1
5 4 8 9 8 7 2
1 5 9 6 3 1 2
Timestamp 2
...
我希望以下列方式导入数据:
我已经实现了1
myData = np.genfromtxt('data.txt', comments='T')
通过这样做,我在Python中有以下内容
1 2 2 3 4 5 6
1 5 8 9 3 4 2
1 2 3 5 1 2 3
5 4 8 9 8 7 2
1 5 9 6 3 1 2
然而,通过这样做,我只是丢弃了所有的时间戳。
但我也需要稍后处理它们。
如何将时间戳导入Python中的下一个列表?
Timestamp 1
Timestamp 2
...
答案 0 :(得分:3)
这个怎么样?
我假设时间戳之前的数字是属于它的数字:
此代码段还会将数字转换为整数。
<强> CODE:强>
with open('source.txt', 'r') as f:
data = {}
numbers = []
for line in f:
ln = line.strip()
if 'Timestamp' in ln:
data[ln] = numbers
numbers = []
elif ln:
numbers.append([int(n) for n in ln.split()])
print(data)
<强>输出:强>
{
'Timestamp 2':
[
[5, 4, 8, 9, 8, 7, 2],
[1, 5, 9, 6, 3, 1, 2]
],
'Timestamp 1':
[
[1, 2, 2, 3, 4, 5, 6],
[1, 5, 8, 9, 3, 4, 2],
[1, 2, 3, 5, 1, 2, 3]
]
}
答案 1 :(得分:1)
@PeterVaro有一个很好的解决方案,可以保存与数据相关联的时间戳,但如果您只想将数字和时间戳导入单独的列表中,您可以这样做:
with open('data.txt') as dataFile:
numbers = []
timestamps = []
for line in dataFile:
# if statement makes sure it's not a blank line with only a newline character in it.
if len(line) > 1:
if 'Timestamp' in line:
timestamps.append(line.rstrip())
else:
numbers.append([int(x) for x in line.split()])
for line in numbers:
for number in line:
print number, " ",
print
print
for timestamp in timestamps:
print timestamp
输出:
1 2 2 3 4 5 6
1 5 8 9 3 4 2
1 2 3 5 1 2 3
5 4 8 9 8 7 2
1 5 9 6 3 1 2
Timestamp 1
Timestamp 2