通过python的CSV迭代而无关的输出



感谢您查看我的问题。

我在Windows 7上使用Python 2.7。我的目标是从原始CSV文件中提取一些数据(ErrorCodehex(。这是原始数据(来自CSV文件(:

AngleInDegrees,DistInMM,Intensity,ErrorCodeHEX
0,2221,259,0
1,2225,244,0
2,2213,172,0
3,2222,98,0
4,2225,87,0
5,2228,82,0
6,2228,84,0
7,2238,82,0
8,2229,64,0
9,2233,44,0
10,2314,17,0
11,2512,8,0
12,0,0,8035
13,0,0,8035
14,0,0,8035
15,0,0,8035
16,0,0,8035
17,0,0,8035
18,0,0,8003
19,0,0,8035
20,0,0,8035

我想提取ErrorCodehex列,并使其看起来像:ErrorCodeHEX | 0 | 0 | 0 |...

但是,我从Python代码中获得的内容包括无关数据(0到359(:

ErrorCodeHEX    
0   0
1   0
2   0
3   0
4   0
5   0
6   0
7   0
8   0
9   0
10  0
11  0
12  8035
13  8035
14  8035

有线是,我尝试了" Distinmm"one_answers"强度"列上的类似代码,它们运行良好。(仅在一行输出列(

所以我感到困惑为什么它不适用于"错误"列?

请参阅代码:

def lds_test(self(:

def my_range(start, end, step):
    while start <= end:
        yield start
        start += step
# gain distance info
for i in my_range(1, len(result) - 3, 3):
    print result[i]
    log.write(result[i] + ',')  
    result[i] += result[i]
# gain intensity info
for i in my_range(2, len(result) - 3, 3):
    print result[i]
    log.write(result[i] + ',')  
    result[i] += result[i]
# gain error info
for i in my_range(3, len(result) - 3, 3):
    print result[i]
    log.write(result[i] + ',')  
    result[i] += result[i]

您可以尝试使用熊猫,它将更快,更容易:

import pandas as pd
df = pd.read_csv('your.csv')
result = "|".join([df.columns[-1]]+map(str, list(df['ErrorCodeHEX'])))
result

输出:

'ErrorCodeHEX|0|0|0|0|0|0|0|0|0|0|0|0|8035|8035|8035|8035|8035|8035|8003|8035|8035'

最新更新