简体   繁体   中英

How can I change date format in csv in Python 3

I'm new to Python, and I have a set of data in a CSV file that I would like to change the format from

'%Y-%m-%dT%H:%MZ' to '%m/%d/%Y'

I'm running Python 3 on Windows. I've searched SO (and other sites) several times but none of the examples/solutions seem to actually convert the format of the output. I've read the Python online documentation but was unable to take anything meaningful away from it.

Here's the code I just tried, and it doesn't change the formatting on any of the entries in the column:

with open('some_file', 'r') as source:
    with open('some_other_file', 'w') as result:
        writer = csv.writer(result, lineterminator='\n')
        reader = csv.reader(source)
        source.readline()
        for row in reader:
            ts = row[17]
            ts = datetime.strptime(ts, '%Y-%m-%dT%H:%MZ').strftime("%m/%d/%Y")
            if ts != "":
                writer.writerow(row)
source.close()
result.close()

I get no errors, but I get no change in the format of the timestamp either.

Suppose you have a date x :

x = "2017-07-01T15:55Z"

You can convert it into a datetime.datetime with your formate %Y-%m-%dT%H:%MZ :

from datetime import datetime
d = datetime.strptime(x, '%Y-%m-%dT%H:%MZ')

Then format it:

d.strftime("%m/%d/%Y")

You'll get:

'07/01/2017'

The complete code is:

from datetime import datetime
x = "2017-07-01T15:55Z"
x = datetime.strptime(x, '%Y-%m-%dT%H:%MZ').strftime("%m/%d/%Y")

======= EDIT =======

For your follow up question: you need to change row after formatting ts :

ts = row[17]
ts = datetime.strptime(ts, '%Y-%m-%dT%H:%MZ').strftime("%m/%d/%Y")
if ts != "":
    row[17] = ts # this is what you miss
    writer.writerow(row)

If I understand your question correctly, you have a string in your CSV file that looks like '2017-08-10T20:47Z' . You should convert this to a datetime.datetime instance with

from datetime import datetime
dt = datetime.strptime('2017-08-10T20:47Z', '%Y-%m-%dT%H:%MZ')

This will give you a datetime.datetime object: datetime.datetime(2017, 8, 10, 20, 47) . You can then reformat it as required with

dts = dt.strftime('%m/%d/%Y')

giving the result '08/10/2017' in dts to write to your updated CSV file.

import csv
from datetime import datetime

with open('some_file.csv', 'r') as source:
    with open('some_other_file.csv', 'w') as result:
        writer = csv.writer(result, lineterminator='\n')
        reader = csv.reader(source)
        source.readline()
        for row in reader:
            ts = datetime.strptime(row[0], '%m/%d/%y %H:%M').strftime("%Y-%m-%d %H:%M:00")
            print(ts)
            row[0]=ts
            if ts != "":
                writer.writerow(row)
source.close()
result.close()

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