簡體   English   中英

Python-如何在文本文件中創建列的滑動窗口?

[英]Python - How do I create a sliding window of columns in a text file?

我有來自傳感器數據的文本文件,例如加速度計和陀螺儀,例如:

% Accelerometer data recorded with 'snsrlog'
% 
% Sampling frequency: 100 Hz
% 
% Label description:
%    0: Default Label
%    1: pick up phone
% 
% Column description:
%    1: Seconds.milliseconds since 1970
%    2: Number of skipped measurements
%    3: Acceleration value in x-direction
%    4: Acceleration value in y-direction
%    5: Acceleration value in z-direction
%    6: Label used for the current sample
% 
% 
1495573445.162   0   0.021973    0.012283    -0.995468   1
1495573445.172   0   0.021072    0.013779    -0.994308   1
1495573445.182   0   0.020157    0.015717    -0.995575   1
1495573445.192   0   0.017883    0.012756    -0.993927   1
1495573445.202   0   0.021194    0.012161    -0.994705   1
1495573445.212   0   0.019638    0.013718    -0.994019   1
1495573445.222   0   0.019440    0.010803    -0.994476   1

文件中還有更多。

我想為加速度計軸的每一列創建一個大小為50的滑動窗口,因為采樣頻率為100Hz。 我可以像這樣打印出第一列的每一行:

def sliding_window(filename, stepSize, windowSize):
    with open(filename) as infile:
        for line in infile:
            print(line.split()[0])

但是,我不應該打印出前17行,因為它們只是文本文件中的注釋。 我將如何制作此滑動窗口?

添加一個startswith條件:

if not line.startswith('%'):
    print(line.split()[0])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM