FLAV10 - 9 months ago 38

Python Question

I have a csv file of data from a LiDAR sensor that looks like this, but with a bagillion more lines:

`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

This is data from a planar sensor. So Scan_0 is a list or "radial" coordinates for the specific scan at a time stamp.

My plan is to:

- Read the CSV file into a list
- Transpose the list
- Turn each element of row into a xyz format like the example below.

`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]

- Write the array of xyz coordinates to a new csv file with one coordinate per line'
- eventually use a trajectory instead of timestamp for the z coordinate from another csv file.

I tell you all of this, so you have some context for my motivation.

here is what I have so far:

`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)

All this really does it ouput the transpose of the source file. It appears that I am having trouble turning the single entry in the list into a list itself. What am I doing wrong here? Any help, advice, and guidance is greatly appreciated.

Answer

The problem that you have is that you are trying to assign new value to variable that does not reference to array.

So you have

```
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
```

You cannot do that, because x is not actual element of `my_r`

list(changing `x`

would not lead to changing element on `my_r`

).

The simplest way is to create new array to store desired values.

```
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)
```

Source (Stackoverflow)