CSV文件没有显示在文件位置



所以我正在编写一个脚本,运行时,模拟运行在AirSim运行。它不断地收集数据(理论上)并将其写入csv文件。然而,当我去到应该保存它的位置时,没有文件。就像即使我保存数据的方法有错误,文件本身也不在那里。

import setup_path
import airsim

import numpy as np
import os
import os.path
import string
import csv
import tempfile
import pprint
import csv
import datetime
client = airsim.MultirotorClient()
client.confirmConnection()
'''
First create a directory for all the csv files to store into.
''' 
dirmain = r"C:AirSimData"
if not os.path.exists(dirmain):
os.mkdir(dirmain)

'''
Create format for file names
'''
run_date_and_time = datetime.datetime.now()
run_date_and_time_string = str(run_date_and_time)
extension = ".csv"
file_name_base = run_date_and_time_string + extension
imu = "imu"
gps = "gps"
magnetometer = "magnetometer"
barometer = "barometer"
gps_file_name = gps + file_name_base
'''Create csv files
'''
gps_file = open(r"gps_file_name",'w')   
gps_header = ['lat','lon','alt']
with open(r"gps_file_name",'w') as gpscsvfile:
gpscsvwriter = csv.writer(gpscsvfile)
gpscsvwriter = gpscsvwriter.writerow(gps_header)    
gpscsvfile.close()

while True: 

#state = client.getMultirotorState()
#s = pprint.pformat(state)
#print("state: %s" % s)

#imu_data = client.getImuData()
#s = pprint.pformat(imu_data)
#print("imu_data: %s" % s)
#barometer_data = client.getBarometerData()
#s = pprint.pformat(barometer_data)
#print("barometer_data: %s" % s)
#magnetometer_data = client.getMagnetometerData()
#s = pprint.pformat(magnetometer_data)
#print("magnetometer_data: %s" % s)
gps_data = client.getGpsData().gnss.geo_point
alt = (gps_data.altitude)
lat = (gps_data.latitude)
lon = (gps_data.longitude)
gps_data_struct = [lat,lon,alt]

with open(r"gps_file_name",'w') as gpscsvfile:
gpscsvwriter = csv.writer(gpscsvfile)
gpscsvwriter = gpscsvwriter.writerow(gps_data_struct)    
gpscsvfile.close()
#print("Altitude: %snLatitude %snLongitude %s" %(alt,lat,lon) )
if False:
break

您正在创建一个文件名,字面意思为"gps_file_name">

with open(r"gps_file_name",'w') as gpscsvfile:
gpscsvwriter = csv.writer(gpscsvfile)
gpscsvwriter = gpscsvwriter.writerow(gps_header)   

您应该使用您创建的带有name元素的变量。Os.path.join()是连接文件名和路径名的安全方法。

gps_file_name = gps + file_name_base    
output_file = os.path.join(dirmain, gps_file_name)
# Should read something like this  "C:AirSimDatagps2021-01-21 13:37:39.867152.csv"

那么你可以在这里使用。

with open(output_file,'w') as gpscsvfile:
gpscsvwriter = csv.writer(gpscsvfile)
gpscsvwriter = gpscsvwriter.writerow(gps_header)    
gpscsvfile.close()

下一个问题是你的datetime字符串包含文件名冒号(:)不能在文件名中使用的无效字符。所以你需要重新考虑这部分。

一个选项是不使用冒号,让你的时间看起来像这样。

run_date_and_time_string = run_date_and_time.strftime('%y-%m-%d_%H%M%S')
# 'C:\AirSimData\gps21-01-21_134531.csv'

相关内容

  • 没有找到相关文章

最新更新