0

I have a csv file that stores dates (format 12 june 2019,), i want to replace a date that a user chooses with 0 (basciaclly deleting it) when ever the function is called. The code runs with no errors however when i check the file again the date hasnt been replaced.

from datetime import datetime


def Remove_event():
    date_string = input("Enter date for event (Format: day month, year): ")

    date_object = datetime.strptime(date_string, "%d %B, %Y")  # converts inputted date into date time data type
    date_object = date_object.strftime("%d %B, %Y")  # formats the date
    date_object = date_object.replace(",", "")  # removes comma in the middle
    date_object1 = date_object + ","  # adds comma at the end

    f = open("dates1.csv", "rt")
    data = f.read()
    data = data.replace(date_object1, '0')
    print("yes")
    f.close()


Remove_event()

1 Answer 1

1

You need to add the following code at the end to actually write the replacement to the file:

fin = open("dates1.csv", "wt")
fin.write(data)
fin.close()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.