刚刚接受了一些帮助,因为我是python的新手。我有一个文件,它是一个输出文件,由外部进程不断写入数据。我打算在此文件中查看' x'一段时间并将输出打印到屏幕和/或将其存储在列表/字典/元组中(不确定python术语中的适当内容)。
这就是我现阶段所拥有的:
timeout = time.time() + args.duration
with open('/dev/input/accel', 'rb') as o:
while time.time() < timeout:
data = o.read(16)
sec, microsec, valtype, axis, axisval = struct.unpack(
'LLHHi', data)
tampertime = float(str(sec) + '.' + str(microsec))
if valtype == 3:
print(tampertime, axis, axisval)
输出就是这个(来自结构):
secs.microsecs, axis, axis value
---------------------------
1478574443.4219799, 0, 4080
1478574443.4219799, 1, 1168
1478574443.4219799, 2, -15408
1478574443.542166, 0, 4016
1478574443.542166, 1, 1104
1478574443.6621571, 0, 4022
1478574443.6621571, 1, 1120
1478574443.6621571, 2, -15404
1478574443.7821031, 0, 4016
1478574443.7821031, 1, 1216
1478574443.7821031, 2, -15430
1478574443.9019749, 0, 4022
1478574443.9019749, 1, 1152
1478574444.2220099, 1, 1148
1478574444.2220099, 2, -15344
注意:我刚刚添加了逗号以便于阅读。
它也打印出其他值,但我只想要valtype == 3
(这是上面输出中没有显示的列)的数字。
要快速解释这三列,第一列是时间(epoch / unix),第二列是&#39;键&#39;第三个是与密钥相关的值。您会注意到,如果键值没有变化,它只是从输出中省略它(我无法修复它,它只是输出文件处理值的方式)。
所以使用上面的输出,我正在寻找这种输出格式。
secs.microsecs, 0, 1, 2
----------------------------------------
1478574443.4219799, 4080, 1168, -15408*
1478574443.542166, 4016, 1104, -15408*
1478574443.6621571, 4022, 1120, -15404
1478574443.7821031, 4016, 1216, -15430*
1478574443.9019749, 4022*, 1152, -15430*
1478574444.2220099, 4022*, 1148, -15344
您会注意到我将旁边的星号放在旁边的值实际上与之前的值相对应(上图)。即,实际上没有为该时间戳提供任何值,因此它应该只使用存储的先前值。
我做了一些搜索并考虑过制作一个结构,但我没有运气。
非常感谢任何帮助。
答案 0 :(得分:1)
您可以使用defaultdict:
from collections import defaultdict
d1=defaultdict(dict)
timeout = time.time() + args.duration
with open('/dev/input/accel', 'rb') as o:
while time.time() < timeout:
data = o.read(16)
sec, microsec, valtype, axis, axisval = struct.unpack(
'LLHHi', data)
tampertime = float(str(sec) + '.' + str(microsec))
if valtype == 3:
d1[tampertime][axis] = axisval # dict with all values
tmp = {0: 0, 1: 0, 2:0}
for k in sorted(d1.keys()): #iterate through dict and customize output format
for j in range(3):
if d1[k].get(j) is None:
d1[k][j] = tmp[j]
tmp = d1[k]
print(k, d1[k])
输出:
1478574443.42198 {0: 4080, 1: 1168, 2: -15408}
1478574443.542166 {0: 4016, 1: 1104, 2: -15408}
1478574443.662157 {0: 4022, 1: 1120, 2: -15404}
1478574443.782103 {0: 4016, 1: 1216, 2: -15430}
1478574443.901975 {0: 4022, 1: 1152, 2: -15430}
1478574444.22201 {0: 4022, 1: 1148, 2: -15344}
答案 1 :(得分:0)
回答我自己的问题。因为我希望在每次执行后打印出值(一旦检索到x,y和z值),我就使用了这个解决方案。我注意到在我的二进制文件中有一个刷新值,我可以轮询并检查以触发打印。
这基本上是我的完整代码:
class Sample:
def __init__(self):
self.x = None
self.y = None
self.z = None
self.ts_sec = None
self.ts_usec = None
def print_val(self):
print("{0}.{1},{2},{3},{4}".format(
self.ts_sec, self.ts_usec, self.x, self.y, self.z))
def DisplaySamples(samples):
for s in samples:
s.print_val()
def UpdateSample(data, sample):
flush = False
sec, usec, valtype, axis, axisval = struct.unpack('LLHHi', data)
sample.ts_sec = sec
sample.ts_usec = usec
if valtype == 3:
if axis == 0:
sample.x = axisval
if axis == 1:
sample.y = axisval
if axis == 2:
sample.z = axisval
if valtype == 0:
flush = True
return sample, flush
def main():
with open('/dev/input/accel', 'rb') as o:
sample = Sample()
while time.time() < timeout:
data = o.read(16)
sample, flush = UpdateSample(data, sample)
if flush:
DisplaySamples([sample])
o.close()
输出示例,csv格式化。
1478733511.251977,2896,-367,None
1478733511.372184,2768,-343,-15648
1478733511.492175,2752,-337,-15776
1478733511.612123,2888,-408,-15648
1478733511.732160,2888,-408,-15728
1478733511.852204,2868,-320,-15608
1478733511.972208,2838,-384,-15624
1478733512.92151,2819,-388,-15624
1478733512.212153,2819,-370,-15856
1478733512.332126,2888,-370,-15664
1478733512.452203,2860,-370,-15776
1478733512.572205,2882,-345,-15608
1478733512.692160,2768,-350,-15760
1478733512.812114,2880,-350,-15760
1478733512.932126,2768,-416,-15696
1478733513.52211,2840,-444,-15690