简体   繁体   English

Python-如何将数据写到新的csv文件中

[英]Python - How to write data out to a new csv file

Ok, so I'm fairly new to Python and I have a .csv file which contains data for racers names, start and finish times. 好的,所以我刚接触Python,我有一个.csv文件,其中包含赛车手姓名,开始和结束时间的数据。 I need to calculate the time taken and output the additional information, plus the earlier data to a new .csv. 我需要计算花费的时间并输出其他信息,以及将较早的数据输出到新的.csv。

This is what I have so far: 这是我到目前为止的内容:

import csv
input_file = csv.DictReader(open("runners_times.csv"))
for row in input_file:
    time_taken = float(row["finish_time"])-float(row["start_time"])
    print (row)
    print (time_taken)

with open("runners_times_appended.csv", "w+") as to_file:
        writer = csv.writer(to_file, delimiter=",")
        for row in input_file:
            writer.writerows(all)

This is creating the new .csv but the file is empty. 这正在创建新的.csv,但文件为空。 Your Help is gratefully appreciated. 非常感谢您的帮助。

I didn't fully understand how are you using time_taken in your file which you are going to save. 我不完全了解如何在要保存的文件中使用time_taken。
As I understand you need to add/change column in your dataFrame/csvFile with time_taken. 据我了解,您需要使用time_taken在dataFrame / csvFile中添加/更改列。 You could do it with pandas package: 您可以使用pandas软件包来做到这一点:

import pandas as pd
df = pd.read_csv("runners_times.csv")
df["time_taken"] = df["finish_time"] - df["start_time"]
df.to_csv("new_name.csv")

Try this: 尝试这个:

import csv
data_list = []
input_file = csv.DictReader(open("runners_times.csv"))
for row in input_file:
    time_taken = float(row["finish_time"])-float(row["start_time"])
    row.update({"time_taken":time_taken})
    data_list.append(row)
keys = data_list[0].keys()
with open("runners_times_appended.csv", "w") as fp:
    dict_writer = csv.DictWriter(fp, keys)
    dict_writer.writeheader()
    dict_writer.writerows(data_list)

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

相关问题 如何在python中“写入新的.CSV文件”或“另存为新的.CSV文件” - How To ' Write To New .CSV File' or "Save As New .CSV File' In python 如何读取 CSV 文件并将大量数据数据以 python 中的块的形式写入新的 CSV 文件 - How to read a CSV file and write a huge data data to a new CSV file in chunks in python 如何使用python对CSV文件进行排序并将排序后的数据写入新的CSV文件 - How to sort a csv file using python and write the sorted data to the new csv file 如何将CSV文件转换成Python中的列表,然后将列表数据写入新的CSV文件? - How can I convert a CSV file into a list in Python and then write the list data to a new CSV file? 如何在Python中将数据写入csv文件 - How to write data to csv file in Python 如何在python中将数据列表写入.csv文件? - How to write lists of data to a .csv file in python? 如何使用python将数据写入CSV文件? - How to write data into a CSV file using python? 使用 python,如何将新项目写入 CSV 文件中的新行 - Using python, how to write new item into a new row in a CSV file 从 Arduino 将新数据写入 Python 中的 csv 文件 - Write new data in to csv file in Python from an Arduino 如何使用python脚本将数据写入现有csv文件中的新列 - How to write the data to the new column in existing csv File using python script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM