繁体   English   中英

如何在 python 的 CSV 文件中写入逗号分隔值?

[英]How to write comma-separated values in a CSV file in python?

with open('value.csv', 'w') as f:
writer = csv.writer(f)


while time < stage1_burn_time:
# Calculate update simulation variables based on current values:
altitude = altitude + (velocity * delta_t)
acceleration = (thrust(altitude, stage1_thrust_sl, stage1_thrust_vac) / mass) - gravity
velocity = velocity + (acceleration * delta_t)
mass = mass - burn_rate * delta_t
time = time + delta_t
writer.writerow(velocity')
writer.writerow('altitude')
writer.writerow('mass')

这是代码的一部分,我想做的是将值记录为 CSV 文件中的一行逗号分隔值,我收到一条错误消息 Error: iterable expected, not float

import csv

with open('values.csv', mode='w') as values_file:
    values_writer = csv.writer(values_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    values_writer.writerow(['velocity1', 'Altitude1', 'Mass1'])
    values_writer.writerow(['velocity2', 'Altitude2', 'Mass2'])

参考: https://realpython.com/python-csv/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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