【问题标题】:How to write comma-separated values in a CSV file in python?如何在 python 的 CSV 文件中写入逗号分隔值?
【发布时间】:2019-10-09 01:42:37
【问题描述】:
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

【问题讨论】:

    标签: python file csv


    【解决方案1】:
    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/

    【讨论】:

      猜你喜欢
      • 2012-08-27
      • 2016-12-08
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 2019-04-05
      相关资源
      最近更新 更多