2016-09-20 62 views
2

我从看起来像这样激光雷达传感器数据的一个csv文件,但有更bagillion行:你如何在一个列表上迭代执行一些将单个元素变为多个的操作?

scan_0,scan_1,scan_2 
timestamp_0,timestamp_1,timestamp_2 
7900200,7900225,7900250 
logTime_0,logTime_1,logTime_2 
27:46.8,27:46.8,27:46.8 
distance_0,distance_0,distance_0 
132,141,139 
136,141,155 
139,141,155 
138,143,155 
138,143,142 
139,143,136 
138,143,136 

这是从一个平面的传感器数据。因此,Scan_0是时间戳特定扫描的列表或“放射状”坐标。

我的计划是:

  1. 阅读CSV文件导入列表
  2. 移调列表
  3. 打开行的每个元素到像下面的例子一个XYZ格式。

    scan_0 -----> scan_0 
    timestamp_0-> timestamp_0 
    7900200-----> 7900200 
    logTime_0---> logTime_0 
    27:46.8-----> 27:46.8 
    distance_0--> distance_0 
    132---------> [132*cos(1),132*sin(1),7900200] 
    136---------> [136*cos(2),136*sin(2),7900200] 
    139---------> [139*cos(3),139*sin(3),7900200] 
    138--------->   .   . 
    138--------->   .   . 
    139--------->   .   . 
    138---------> [138*cos(7),139*sin(7),7900200] 
    
  4. 每行写”

  5. 与一个坐标,最终使用的轨迹,而不是时间戳用于z从另一个csv文件的坐标XYZ坐标的数组到一个新的CSV文件。

我告诉你所有这些,所以你对我的动机有一些背景。

这里是我到目前为止有:

import csv 
import numpy 
from numpy import genfromtxt 
from numpy import vsplit 
import math 

with open('2016_09_16_14_29_09_953.csv',"r") as f: 
    reader = csv.reader(f,delimiter = ",") 
    data = list(reader) 
    row_count = len(data) 
print row_count 
with open("out.csv", "a") as f1: 
    my_r = genfromtxt('2016_09_16_14_29_09_953.csv', delimiter=',', skip_header=6, autostrip=True) #import data 
    my_r = my_r.T #transpose 
    for x in my_r: 
     i=0 
     while i < row_count/360: 
      x = [x*math.cos(i), x*math.sin(i), i] 
      i = i + row_count/360 

    thedatawriter = csv.writer(f1) #setup writer 
    for row in my_r: #write the data 
     thedatawriter.writerow(row) 

这一切确实是输出中的源文件的转置。看来我无法将列表中的单个条目本身变成列表。我在这里做错了什么?任何帮助,建议和指导,非常感谢。

回答

1

您遇到的问题是您尝试将新值分配给未引用数组的变量。

所以,你必须

for x in my_r: 
    i=0 
    while i < row_count/360: 
     x = [x*math.cos(i), x*math.sin(i), i] 
     i = i + row_count/360 

你不能这样做,因为x不是my_r列表(改变x不会导致对my_r改变元件)的实际元素。

最简单的方法是创建新的数组来存储所需的值。

my_r = genfromtxt('2016_09_16_14_29_09_953.csv', delimiter=',', skip_header=6, autostrip=True) #import data 
my_r = my_r.T #transpose 
new_r = [] 
for x in my_r: 
    i=0 
    while i < row_count/360: 
     new_r.append([x*math.cos(i), x*math.sin(i), i]) 
     i = i + row_count/360 
thedatawriter = csv.writer(f1) #setup writer 
for row in new_r: #write the data 
    thedatawriter.writerow(row) 
相关问题