Beckhoff TwinCat Scope CSV 格式到 pandas 数据帧

Beckhoff TwinCat Scope CSV Format into pandas dataframe

在 Beckhoff TwinCAT Scope 中记录数据后,可以将此数据导出到 CSV 文件。然而,所述 CSV 文件具有相当复杂的格式。谁能建议将此类文件导入 pandas Dataframe 以便我执行分析的最有效方法?

格式示例可在此处找到: https://infosys.beckhoff.com/english.php?content=../content/1033/tcscope2/html/TwinCATScopeView2_Tutorial_SaveExport.htm&id=

要读取大部分文件(忽略 header material),请使用 skiprows 关键字参数 read_csv:

import pandas as pd
df = pd.read_csv('data.csv', skiprows=18)

对于 header material,我认为您必须编写自定义解析器。

无需编写自定义解析器。使用示例数据 scope_data.csv:

Name,fasd,,,,
File,C;\,,,,
Start,dfsd,,,,
,,,,,
,,,,,
Name,Peak,Name,PULS1,Name,SINUS_FAST
Net id,123.123.123,Net id,123.123.124,Net Id,123.123.125
Port,801,Port,801,Port,801
,,,,,
0,0.6113936598,0,0.07994111349,0,0.08425652468
0,0.524852539,0,0.2051963401,0,0.4391185847
0,0.4993723482,0,0.2917317117,0,0.4583736263
0,0.5976553194,0,0.8675482865,0,0.8435987898
0,0.06087224998,0,0.7933980583,0,0.5614294705
0,0.1967968423,0,0.3923966599,0,0.1951608414
0,0.9723649064,0,0.5187276782,0,0.7646786192

您可以导入如下:

import pandas as pd

scope_data = pd.read_csv(
    "scope_data.csv",
    skiprows=[*range(5), *range(6, 9)],
    usecols=[*range(1, 6, 2)]
)

然后你得到

>>> scope_data.head()
       Peak     PULS1  SINUS_FAST
0  0.611394  0.079941    0.084257
1  0.524853  0.205196    0.439119
2  0.499372  0.291732    0.458374
3  0.597655  0.867548    0.843599
4  0.060872  0.793398    0.561429

我没有原始范围 csv,但稍微调整 skiprowsuse_cols 应该会给你想要的结果。