Mobs
Mobs

Reputation: 15

Formatting Time, Python

How do I format a date and time timestamp???

Here is some sample data, "data1.csv":

Mary,11/13/16 10:38
John,11/14/16 12:53
Paul,11/13/16 14:45
Ringo,11/13/16 12:17
George,11/14/16 10:15

I'd like to create a file with three columns. The first column is the name. The second column is just the date 'mm/dd/yy'. The third column is only the time 'HH:MM', in military time.

Here's the code I'm working with and would like to keep.

def process_data(raw_data):
    with open(raw_data) as inputs:
        for line in inputs:
            name, time = line.strip().split(",")
            mylist = list()
            mylist.append(name)
            mylist.append(time)    #As 'mm/dd/yy'
            mylist.append(time)    #As 'HH:MM'
            print ",".join(mylist)

if __name__ == "__main__":
    process_data("data1.csv")

Also, the correct way to save this to a csv file would help my sanity.
The final file should have:

Mary,11/13/16,10:38
John,11/14/16,12:53
Paul,11/13/16,14:45
Ringo,11/13/16,12:17
George,11/14/16,10:15

Upvotes: 0

Views: 116

Answers (3)

Mat
Mat

Reputation: 1403

Something like this will do the job.

from datetime import datetime
import tablib

DATE_FORMAT = '%m/%d/%y'
TIME_FORMAT = '%H:%M'

data = [
    ('Mary', '11/13/16 10:38'),
    ('John', '11/14/16 12:53'),
    ('Paul', '11/13/16 14:45'),
    ('Ringo', '11/13/16 12:17'),
    ('George', '11/14/16 10:15'),
]

results = tablib.Dataset()

for item in data:
    name, datetime_string = item
    _datetime = datetime.strptime(datetime_string, '{} {}'.format(DATE_FORMAT, TIME_FORMAT))
    date = _datetime.strftime(DATE_FORMAT)
    time = _datetime.strftime(TIME_FORMAT)

    results.append((name, date, time))

print(results.csv)

Upvotes: 0

jsf80238
jsf80238

Reputation: 1673

$ cat input
Mary,11/13/16 10:38
John,11/14/16 12:53
Paul,11/13/16 14:45
Ringo,11/13/16 12:17
George,11/14/16 10:15

$ cat time_.py
import csv
with open("input") as reader, open("output.csv", "w") as writer:
    writer = csv.writer(writer, delimiter=",", quoting=csv.QUOTE_NONE)
    for line in reader.readlines():
        name, time = line.strip().split(",")
        day, clock = time.split()
        writer.writerow((name, day, clock))

$ python time_.py

$ cat output
Mary,11/13/16,10:38
John,11/14/16,12:53
Paul,11/13/16,14:45
Ringo,11/13/16,12:17
George,11/14/16,10:15

Upvotes: 0

Chris Mueller
Chris Mueller

Reputation: 6680

If all you really want to do is replace the space with a comma, then the simplest method, as noted in the comments, would be

line.strip().replace('', ',')

You could use Python's built-in datetime package to convert each string to a datetime object. Then you can extract the parts you want when writing them to your new file.

import datetime.datetime as dt

name, time_string = line.strip().split(',')
time_object = dt.strptime(time_string, '%m/%d/%y %H:%M')
date = time_object.strftime('%m/%d/%y')
time = time_object.strftime('%H:%M')

This would give you the flexibility to easily change the formatting if you so desire.

Upvotes: 1

Related Questions