0

I have a CSV file (calendar), 5 columns that I want to read and parse with the following conditions using a script:

  • Deleting headers (done)
  • Change the format of the first column from 01/01/2019 to 20190101 in the First column

The first part of the script is done to skip headers. The second part I think a regex is required but I just don't know how to first remove the / and then move the 0101 from before 2019 to after 2019 so that the result is 20190101

If someone could help that would be great!

def parse_calendar(infile, outfile):
    with open(outfile, 'w', newline='') as output:
        with open(infile, newline='') as input:
            reader = csv.reader(input, delimiter=',', quotechar='"')
            next(reader, None)  # skip the headers
            writer = csv.writer(output, delimiter=',', quotechar='"')
            for row in reader:   # process each row
                writer.writerow(row)

I expect the output to be like the following compared to the initial file:

01/01/2019 New Year's Day N C US

20190101 New Year's Day N C US

4
  • possible duplicate of How can pass the current datetime to string in python? You can convert the timestamp to a string and then can use the replace() method to change the / : to `` or backspace Commented May 29, 2019 at 22:39
  • It’s not a duplicate. The column is part of a csv file and is not a timestamp type. Plus also if you remove the ‘/‘ , I still need something that moves the first 4 characters after the last one, meaning 01012019 has to become 20190101. And would be best if it’s code added to the script I already have. This action is performed on the first column in the file. Thanks! Commented May 29, 2019 at 22:54
  • I don't see eye to eye with you. Nevertheless you could use strftime() and modify it from ("%Y-%m-%d %H-%M-%S") to any form you want to. Commented May 29, 2019 at 23:29
  • 1
    @V.Montréal seems like you already have a way to do most of what you want. I'd say your question boils down to just "how to change the format of a date string in python?" Commented May 29, 2019 at 23:33

1 Answer 1

0

Thanks guys for the responses.

So with this code I get the following output:

import csv

def parse_calendar(infile, outfile):
    with open(outfile, 'w', newline='') as output:
        with open(infile, newline='') as input:
            reader = csv.reader(input, delimiter=',', quotechar='"')
            next(reader, None)  # skip the headers
            writer = csv.writer(output, delimiter=',', quotechar='"')
            for row in reader: # process each row
                replaced = row[0].replace('/','')  
                row[0] = replaced
                writer.writerow(row)

01012018,New Year's Day,N,C,US

01012018,New Year's Day,N,C,CA

01152018,Martin L. King Day,N,C,US

What code do I need to add to the script get the formatting different now from 01012018 to 20180101 given the type is a String? For each line then off course.

Appreciate it alot thanks

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.