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.