1

Coding in Python

I have a CSV file with a column called time that consists of strings like 12:00:00 AM and 10:00:00 PM. I simply want to convert these strings into their corresponding military time representation either by string or integer such that 12:00:00 AM becomes "0" or 0 and 10:00:00 PM becomes "22" or 22.

I am new to coding in general so I have no idea what are the correct keywords to search for. Thanks!

Whooper
  • 575
  • 4
  • 20
  • Happy Coding. SO is about fixing _your_ Code - not implementing your ideas. Please go over [how to ask](https://stackoverflow.com/help/how-to-ask) and [on-topic](https://stackoverflow.com/help/on-topic) again and if you have questions provide your code as [mvce](https://stackoverflow.com/help/mcve). – Patrick Artner Jan 31 '18 at 19:52
  • Questions looking for offsite resources are offtopic, but you can start to read here: [time.strptime](https://docs.python.org/3/library/time.html#time.strptime) or simply search SO with "python parse time" f.e starting to read [this](https://stackoverflow.com/questions/466345/converting-string-into-datetime/466376#466376) – Patrick Artner Jan 31 '18 at 19:55
  • 2
    Possible duplicate of [Convert 12 hour into 24 hour times](https://stackoverflow.com/questions/19229190/convert-12-hour-into-24-hour-times) – melwil Jan 31 '18 at 20:06

2 Answers2

0
import datetime

t = '12:00:00 AM'

t_dt = datetime.datetime.strptime(t, '%I:%M:%S %p')

then you can access the hour with t_dt.hour

Taylor
  • 378
  • 2
  • 4
  • 14
0

Try coding it first by yourself and update your question by posting your code when you encounter errors with it.

To give you a head start, you can start by reading the time module of python. In particular, you will be needing the time.strptime to convert your string format to a time format then output your desired military time format using time.strftime.

Make sure to check out the directives properly for the formatting of your strings. Happy coding!

Whooper
  • 575
  • 4
  • 20
  • Just to add, you may use [this](https://codereview.stackexchange.com/posts/137906/revisions) as a reference while you code. – Whooper Jan 31 '18 at 20:14