簡體   English   中英

從 CSV 將值讀入 python,創建新值,如何將其添加到新行?

[英]Read value into python from CSV, created new value, how do I add it to new row?

python 和一般編程非常新。 我正在嘗試創建一個腳本,該腳本從 csv 獲取開始時間和結束時間,以分鍾為單位計算持續時間,然后將值寫入新的 csv。

with open('testdata1.csv','r') as csv_file:
        csv_reader = csv.DictReader(csv_file)

        with open('new_testdata1.csv','w') as new_file:
            fieldnames = ['Title', 'Description', 'Location', 'Start', 'End',
                          'Date', 'Duration_minutes']

            csv_writer = csv.DictWriter(new_file, fieldnames = fieldnames)

            csv_writer.writeheader()


        for line in csv_reader:
            start = line['Start']
            end = line['End']
            date = convertdate(end)
            end = converttime(end)
            start = converttime(start)
            duration = end - start
            print(duration)

如何將持續時間變量寫入持續時間字段?

非常感謝

如有必要,完整的腳本供參考

total.py
# fully functioning script, need to convert into function still

import datetime
import csv

def converttime(string):

    #convert string to standard format
    FullDate = datetime.datetime.strptime(string, "%B %d, %Y at %I:%M%p")

    #convert variable to string
    FullDate = str(FullDate)

    #slice out time
    time = FullDate[11:16]

    #convert time to minutes
    hr = int(time[0:2])
    mins = int(time[3:5])
    minutes = hr * 60 + mins
    return minutes

def convertdate(string):

    #convert string to standard format
    FullDate = datetime.datetime.strptime(string, "%B %d, %Y at %I:%M%p")

    #convert variable to string
    FullDate = str(FullDate)

    #slice out date
    date = FullDate[0:10]
    return date


def main():
    #get string
    with open('testdata1.csv','r') as csv_file:
        csv_reader = csv.DictReader(csv_file)

        with open('new_testdata1.csv','w') as new_file:
            fieldnames = ['Title', 'Description', 'Location', 'Start', 'End',
                          'Date', 'Duration_minutes']

            csv_writer = csv.DictWriter(new_file, fieldnames = fieldnames)

            csv_writer.writeheader()


        for line in csv_reader:
            start = line['Start']
            end = line['End']
            date = convertdate(end)
            end = converttime(end)
            start = converttime(start)
            duration = end - start
            print(duration)


main()

這里你的duration_time是你每行的持續時間

from csv import writer
from csv import reader

duration_time = 'Some Text'
# Open the input_file in read mode and output_file in write mode
with open('input.csv', 'r') as read_obj, \
        open('output_1.csv', 'w', newline='') as write_obj:
    # Create a csv.reader object from the input file object
    csv_reader = reader(read_obj)
    # Create a csv.writer object from the output file object
    csv_writer = writer(write_obj)
    # Read each row of the input csv file as list
    for row in csv_reader:
        # Append the duration in the row / list
        row.append(duration_time)
        # Add the updated row / list to the output file
        csv_writer.writerow(row)

這將為每一行添加duration_time

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM