-1

i am tring to save imu and gnss data to a csv file from carla simulator , i can read the datas from the terminal but can not save them as csv file it gives an error AttributeError: 'list' object has no attribute 'to_csv ', i created an empty list then tried to save then convert to csv file

actor_list = []
try:
    
    # Add GNSS sensor to ego vehicle. 
    # --------------

    gnss_bp = world.get_blueprint_library().find('sensor.other.gnss')
    gnss_location = carla.Location(0,0,0)
    gnss_rotation = carla.Rotation(0,0,0)
    gnss_transform = carla.Transform(gnss_location,gnss_rotation)   
    gnss_bp.set_attribute("sensor_tick",str(3.0))
    ego_gnss = world.spawn_actor(gnss_bp,gnss_transform,attach_to=ego_vehicle, attachment_type=carla.AttachmentType.Rigid)
    def gnss_callback(gnss):
        print("GNSS measure:\n"+str(gnss)+'\n')
    ego_gnss.listen(lambda gnss: gnss_callback(gnss))
    actor_list.append(ego_gnss)
# --------------
# Add IMU sensor to ego vehicle. 
# --------------

    imu_bp = world.get_blueprint_library().find('sensor.other.imu')
    imu_location = carla.Location(0,0,0)
    imu_rotation = carla.Rotation(0,0,0)
    imu_transform = carla.Transform(imu_location,imu_rotation)
    imu_bp.set_attribute("sensor_tick",str(3.0))
    ego_imu = world.spawn_actor(imu_bp,imu_transform,attach_to=ego_vehicle, attachment_type=carla.AttachmentType.Rigid)
    def imu_callback(imu):
        print("IMU measure:\n"+str(imu)+'\n')
    ego_imu.listen(lambda imu: imu_callback(imu))


    actor_list.append(ego_imu)
    #add this sensor to our actors.

    time.sleep(10)
    # sleep for 5 seconds, then finish:
    data =[]
    low_data = {        "accelX":ego_imu.accelerometer[0],
                        "accelY":ego_imu.accelerometer[1],
                        "accelZ":ego_imu.accelerometer[2],
                        "gyroX":ego_imu.gyroscope[0], 
                        "gyroY":ego_imu.gyroscope[1], 
                        "gyroZ":ego_imu.gyroscope[2],
                        "Lat": ego.gnss[0],
                        "Lon" : ego.gnss[1]}
    data = data.append(low_data, ignore_index=True)
    print(low_data)

finally:
    data.to_csv("out_{}.csv".format(args.name))
    print("CSV SAVED FOR DRIVER named {}".format(args.name))




1 Answers1

0

Use the csv module as found https://www.geeksforgeeks.org/python-save-list-to-csv/

You once you have the list formed you want to:


    fields = ['Name', 'Branch', 'Year', 'CGPA'] 
    
    # data rows of csv file 
    rows = [ ['Nikhil', 'COE', '2', '9.0'], 
         ['Sanchit', 'COE', '2', '9.1'], 
         ['Aditya', 'IT', '2', '9.3'], 
         ['Sagar', 'SE', '1', '9.5'], 
         ['Prateek', 'MCE', '3', '7.8'], 
         ['Sahil', 'EP', '2', '9.1']] 
    with open('file_name.csv', 'w') as f:
      
       # using csv.writer method from CSV package
       write = csv.writer(f)
      
       write.writerow(fields)
       write.writerows(rows)

Essentially you are creating the file and then manually writing it out with each item in the list.

Another way you could do it is if you have the information saved in a dictionary is use:

    
    def to_csv(self):
            '''
            Save data to csv file
            '''
    
            keys = self.data[0].keys()
            with open("sample.csv", "w", newline='')  as csv_output:
                dict_writer = csv.DictWriter(csv_output, keys)
                dict_writer.writeheader()
                dict_writer.writerows(self.data)

Kwsswart
  • 529
  • 1
  • 7
  • 20