简体   繁体   中英

How to read contents of a large csv file and write them to another file with an operator in Python?

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:

  1. Read the CSV file into a list
  2. Create Separate Array's for each scan (columns)
  3. Turn each element of the scan array 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]
  4. Write the array of xyz coordinates to a new csv file with one coordinate per line'

  5. 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. To start this little project, I've decided to simply try and read the csv file into a list and output each of the lines to another csv file... start small right?

here is what I have so far:

import csv 

with open("2016_09_16_14_29_09_953.csv", 'r') as f:
    with open("out.csv", "a") as f1:
        x = csv.reader(f)
        my_list = list(x)
        thedatawriter = csv.writer(f1)
        for row in my_list:
            thedatawriter.write(row)

This creates an empty csv file. What am I doing wrong here? I feel like an ant climbing mount Everest. Any help, advice, and guidance is greatly appreciated.

I am not sure why this isn't blowing up on you. csv.writer objects don't have a write function. Try thedatawriter.writerow(row) instead.

When running your code, I get the following error:

AttributeError: '_csv.writer' object has no attribute 'write'

Are you sure you are not getting the same kind of error? Because based on the documentation of csv , the method seems to be writerow instead of write .

So, like Rob Davis already seems to have answered, thedatawriter.writerow(row)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM