简体   繁体   中英

Reading and parsing a CSV file in Python with Regex first column

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 NC US

20190101 New Year's Day NC US

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM