Creating CSV and adding timestamp
- Import csv and datetime module. We will use the csv module to read and write the csv file and datetime module to add the current date and time in the csv file
- Take the data from the user.
- Open the CSV file in read and write mode (‘r+’) using open() function.
- The open() function opens a file and returns its as a file-object.
- newline = ‘ ‘ controls how universal newlines mode works. It can be None, ‘ ‘, ‘\n’, ‘\r’, and ‘\r\n’.
- write() returns a writer object which is responsible for converting the user’s data into a delimited string.
- Get current date and time using the datetime.now() function of datetime module.
- Iterate over all the data in the rows variable with the help of a for loop.
- Insert the current date and time at 0th index in every data using the insert() function.
- Write the data using writerow() in the CSV file with the current date and time.
Example 1: Add timestamp to CSV file
Python3
# Importing required modules import csv from datetime import datetime # Here we are storing our data in a # variable. We'll add this data in # our csv file rows = [[ 'w3wiki1' , 'w3wiki2' ], [ 'w3wiki3' , 'w3wiki4' ], [ 'w3wiki5' , 'w3wiki6' ]] # Opening the CSV file in read and # write mode using the open() module with open (r 'YOUR_CSV_FILE.csv' , 'r+' , newline = '') as file : # creating the csv writer file_write = csv.writer( file ) # storing current date and time current_date_time = datetime.now() # Iterating over all the data in the rows # variable for val in rows: # Inserting the date and time at 0th # index val.insert( 0 , current_date_time) # writing the data in csv file file_write.writerow(val) |
Output :
Example 2: Adding timestamp to CSV file
Python3
# Importing required modules import csv from datetime import datetime # function to write in csv file def write_in_csv(rows): # Opening the CSV file in read and # write mode using the open() module with open (r 'YOUR_CSV_FILE.csv' , 'r+' , newline = '') as file : # creating the csv writer file_write = csv.writer( file ) # Iterating over all the data in the rows # variable for val in rows: # writing the data in csv file file_write.writerow(val) # list to store the values of the rows rows = [] # while loop to take # inputs from the user run = '' while run ! = 'no' : # lists to store the user data val = [] # Taking inputs from the user val1 = input ( "Enter 1st value:- " ) val2 = input ( "Enter 2nd value:- " ) val3 = input ( "Enter 3rd value:- " ) # storing current date and time current_date_time = datetime.now() # Appending the inputs in a list val.append(current_date_time) val.append(val1) val.append(val2) val.append(val3) # Taking input to add one more row # If user enters 'no' then the will loop will break run = input ( "Do you want to add one more row? Type Yes or No:- " ) run = run.lower() # Adding the stored data in rows list rows.append(val) # Calling function to write in csv file write_in_csv(rows) |
Output:
How to add timestamp to CSV file in Python
Prerequisite: Datetime module
In this example, we will learn How to add timestamp to CSV files in Python. We can easily add timestamp to CSV files with the help of datetime module of python. Let’s the stepwise implementation for adding timestamp to CSV files in Python.