Python: 写入 csv 文件的值顺序相反?
Python: values written to csv file are in reverse order?
首先是有问题的代码:
def write_to_csv(current_time_in_utc: datetime, time: int, start_latitude: float, stop_latitude: float, current_lat=None, step=None) -> None:
if not exists('/app/pan_tilt_unit/value_recording/position_values.csv'):
create_csv()
# Use some memoization here so we don't have to calculate the step each loop
# Should probably check if accessing a dict value by key is faster than the simple calculation - might benchmark it
if not step:
step = approximate_position_steps(start_latitude, stop_latitude, time)
step_for_memoization[step] = step
if not current_lat:
current_lat = start_latitude
if time >= 0:
with open('/app/pan_tilt_unit/value_recording/position_values.csv', 'a') as csv_file:
csv_writer = writer(csv_file)
csv_writer.writerow([current_time_in_utc, current_lat])
logger.info("Writing values to file.")
sleep(1)
write_to_csv(datetime.utcnow(), time-1, start_latitude, stop_latitude, current_lat=current_lat+step, step=step_for_memoization[step])
else:
logger.debug("Finished writing values to file.")
step_for_memoization.clear()
return
这是一个示例输出:
time_in_UTC,current_latitude,current_longitude
2022-04-01 03:23:13.166506,142.34200000000007
2022-04-01 03:23:12.165016,141.59500000000006
2022-04-01 03:23:11.162850,140.84800000000004
2022-04-01 03:23:10.161289,140.10100000000003
2022-04-01 03:23:09.159162,139.354
2022-04-01 03:23:08.156691,138.607
为什么csv写反了?时间戳应该从 03:23:08 到 03:23:13,但它不是..我想这可能是因为我正在递归调用它?但不完全确定。 with open() 中的 'a' 用于 'append',因此从逻辑上讲,它应该在每个值返回时一个接一个地附加它们。
从with open
中取消递归调用,这样缓冲写入的行实际上flushed/written到文件之前递归调用而不是在 递归调用返回后。
首先是有问题的代码:
def write_to_csv(current_time_in_utc: datetime, time: int, start_latitude: float, stop_latitude: float, current_lat=None, step=None) -> None:
if not exists('/app/pan_tilt_unit/value_recording/position_values.csv'):
create_csv()
# Use some memoization here so we don't have to calculate the step each loop
# Should probably check if accessing a dict value by key is faster than the simple calculation - might benchmark it
if not step:
step = approximate_position_steps(start_latitude, stop_latitude, time)
step_for_memoization[step] = step
if not current_lat:
current_lat = start_latitude
if time >= 0:
with open('/app/pan_tilt_unit/value_recording/position_values.csv', 'a') as csv_file:
csv_writer = writer(csv_file)
csv_writer.writerow([current_time_in_utc, current_lat])
logger.info("Writing values to file.")
sleep(1)
write_to_csv(datetime.utcnow(), time-1, start_latitude, stop_latitude, current_lat=current_lat+step, step=step_for_memoization[step])
else:
logger.debug("Finished writing values to file.")
step_for_memoization.clear()
return
这是一个示例输出:
time_in_UTC,current_latitude,current_longitude
2022-04-01 03:23:13.166506,142.34200000000007
2022-04-01 03:23:12.165016,141.59500000000006
2022-04-01 03:23:11.162850,140.84800000000004
2022-04-01 03:23:10.161289,140.10100000000003
2022-04-01 03:23:09.159162,139.354
2022-04-01 03:23:08.156691,138.607
为什么csv写反了?时间戳应该从 03:23:08 到 03:23:13,但它不是..我想这可能是因为我正在递归调用它?但不完全确定。 with open() 中的 'a' 用于 'append',因此从逻辑上讲,它应该在每个值返回时一个接一个地附加它们。
从with open
中取消递归调用,这样缓冲写入的行实际上flushed/written到文件之前递归调用而不是在 递归调用返回后。