Jordy
Jordy

Reputation: 39

How to split elements in a list and get date and time from a list

Since the beginning of time I have been struggling with importing files in Python and reading lines in a optimal way. For example, a file I have is as follows:

2015 02 25    09:00:00
A second line
One more line

Now I want to extract the date and time from the first line; for this we want it in this format I think, to make it work in the datetime module

(2015,02,25,09,00,00)

This is what I have

with open('file.txt', newline='') as inputfile:
  data = inputfile.readlines()
  print(data[0])
Out: ['2015 02 25    09:00:00']

This gives us the first element of the list. Now I want to make a comma separated list out of this. Now when I try this for example:

In: datetime = [i.split(':') for i in file[0]]
Out: [['2015 02 25    09', '00', '00']]

I get a list of lists, which does not make things easier in any way. And we haven't even split the whitespaces yet. What is the best way to get the date and time out of this? And more in general, do you know any good tutorials to practice list/string splitting, iterate over text files/lists etc.

Upvotes: 2

Views: 3482

Answers (5)

eliasmaxil
eliasmaxil

Reputation: 558

Quite late to the discussion but if another answer can help, why not. Let's say you have a file called dates.txt with some timestamps in it like the following.

2021-01-01 00:00:00
2021-02-01 12:00:00
2021-12-12 23:59:59

You could read line by line and parse the strings to obtain a list of integers or a datetime object (depending on what you prefer) with something like the following code

import datetime as dt
with open("dates.txt", 'r') as f:
    for _, line in enumerate(f):
        line_str = line.split(" ")[0].split("-") + line.split(" ")[1].split(":")
        line_int = [int(i) for i in line_str] # Creates a list of integers from the string
        print(line_int)
        line_dt = dt.datetime(*line_int) # Creates a datetime object from the list of integers
        print(line_dt)

So for each line

  • First split in the empty space and then in "-" and ":"
  • Parse the splitted strings into integers With that you could get a list of integers representing the timestamp. If you want to convert then to a datetime object, you could expand the list first.

Of course there are other methods that could be faster.

I hope it can still be helpful to somebody.

Upvotes: 0

Brent
Brent

Reputation: 778

why not just read in every line one by one and just append each line to a list?

mylist = []
with open(filename) as f:
    for line in f.readlines():
        mylist.append(line.strip("\n")) \\strip to get rid of the new line from the file
    print mylist

this would read every line in the file separate then add each line to a comma separated list

is this what your looking for?

output would look like this

['2015 02 25    09:00:00', '2015 02 27    09:10:00', '2015 02 29    09:20:00']

thats from 3 lines in the file but I just used the same entry if you had 3 different entries in the file it would put the different ones into the list

I don't think anyone here actually knows what you want if you could put up the end result you are looking for that would help everyone out a lot and get you an answer a lot faster

Upvotes: 0

Saketh Katari
Saketh Katari

Reputation: 352

Split the line by spaces .split(" ") and Extract year, month and day from the first 3 strings. Now, the last string has the time, split it with : i.e .split(":") to extract hours, minutes and seconds.

f = open("sample.txt", "r")

data = f.read()
lines = data.split("\n")
first_line = lines[0]

str_arr = first_line.split(" ")
year = int(str_arr[0])
month = int(str_arr[1])
day = int(str_arr[2])
print("year : " + str(year))
print("Month : " + str(month))
print("Day : " + str(day))

last_index = len(str_arr)-1
time = str_arr[last_index].split(":")
hours = int(time[0])
minutes = int(time[1])
seconds = int(time[2])
print("Hours : " + str(hours))
print("Minutes : " + str(minutes))
print("seconds : " + str(seconds))

f.close()

Upvotes: 0

Jean-François Fabre
Jean-François Fabre

Reputation: 140196

I suppose that you want this:

line="2015 02 25    09:00:00\n"   # here line is data[0]

toks = line.split()
date = " ".join(toks[:3])
time = toks[-1]   # you can split it according to ":" now if you want

print(date)
print(time)

prints:

2015 02 25
09:00:00

split the tokens according to 1-n blanks, then join the 3 first tokens to get the date, and get last element to get the time.

Python 3 allows star unpacking so you can directly do this:

*datetoks,time = line.split()
date = " ".join(datetoks)

(datetoks contains all the tokens but the last one thanks to *)

Upvotes: 0

Mike Scotty
Mike Scotty

Reputation: 10782

To get the date and time (or rather the datetime) from your input, it's easiest to use strptime.

import datetime

s = "2015 02 25    09:00:00"

dt = datetime.datetime.strptime(s, "%Y %m %d %H:%M:%S")

print(dt)

Output:

2015-02-25 09:00:00

You don't even have to bother with the exact amount of spaces in the "format" string.

You can then convert/format the datetime object as needed.

Upvotes: 5

Related Questions