-1
Crying time
20:31:47    
23:33:46
10:20:00
11:30:00
12:15:00
20:31:47
23:33:46
 10:20:00
 11:30:00
 12:15:00
 17:45:00
 19:20:00
 19:45:00
 22:30:00
  0:22:03
11:30:00
12:15:38
15:01:06
18:12:21 
20:56:39
23:30:00
11:40:00
13:30:00
18:29:42
21:15:41

How to extract the time from one column to different separate column in hours, minute, and second in Python?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • Can you give the output sample? And are you ok with using `pandas`? – ggaurav Jan 10 '21 at 06:28
  • [basic string manipulation](https://docs.python.org/3/library/stdtypes.html#str.split) or [regex](https://docs.python.org/3/library/re.html) should suffix, no need for machine-learning. – KaiserKatze Jan 10 '21 at 06:56
  • Does this answer your question? [How to split a string into a list?](https://stackoverflow.com/questions/743806/how-to-split-a-string-into-a-list) – KaiserKatze Jan 10 '21 at 06:58
  • As @ggaurav said, an output example of what you want would be really helpful to understand your question. Please, update your question to clarify this. – gorandp Jan 10 '21 at 10:50

3 Answers3

0

Is that what you want?

Your "dat" file:

Crying time
20:31:47    
23:33:46
10:20:00
11:30:00
12:15:00

The code:

import re

with open("dat", "r") as msg:
    text = msg.readlines()

text = re.sub(r"[A-Za-z]","","".join(text)).replace(":"," ")

print (text)

Output:

20 31 47    
23 33 46
10 20 00
11 30 00
12 15 00

Your text data are in a "dat" file. You read the file line by line in a list using the readlines() method.

Then, you remove your header by using the re.sub method which replaces letters by nothing over the join product of your list (which yields a string). Finally, you replace the ":" by a space to print the output.

Alternatively, you can do:

with open("dat", "r") as msg:
    text = msg.readlines()

for i in range(1,len(text)):
    print (text[i].strip().replace(":"," "))

Which yields the same output.

Synthase
  • 5,849
  • 2
  • 12
  • 34
  • ya in that way....but its very confusing ...i didnt understand it anything.....like i have a column name Crying_time and i want to slit the hour, min,and sec in differnt column of each ....then how to do it ? – Biswajit Kr Singh Jan 10 '21 at 06:25
0

I don't know what data you're using as its not formatted correctly, but if it was a string you can use .split to separate each hour minute and second into a list, which you can then call one each value

Crying_time = "20:31:47"
print(Crying_time.split(":"))

Would output

['20', '31', '47']

And can be seen by using

print(Crying_time[0])

Would output

'20'

If your numbers arent a string you can use str() to convert them to a string.

x = 1234
x = str(x)

frogs114
  • 13
  • 3
0

As I understand. You have a file to read. Let's name it imsad.help. This file has this content:

Crying time
20:31:47    
23:33:46
10:20:00
11:30:00
12:15:00
20:31:47
23:33:46
 10:20:00
 11:30:00
...(continues)

Then, I understand you want to separate into 3 vectors:

  • hours
  • minutes
  • seconds

So we can store these values in 3 different lists. The code may look something like:

hours = []
minutes = []
seconds = []

with open("imsad.help", "r") as fp:
    for line in fp: # Read the file, line by line
        t = line.strip() # strip removes whitespaces and new lines
        t = t.split(":") # split creates a list separating elements with ':'
                         # in between. In our case, it should create a list
                         # like: [hour, min, sec]

        if len(t) != 3:
            continue # This is the first row that contains ["Crying time"]
                     # So, we skip this and continue with other lines

        # cast to int and appends to each list
        hours.append(int(t[0]))
        minutes.append(int(t[1]))
        seconds.append(int(t[2]))

# Print only for demo
print("hours:", hours, "\n")
print("minutes:", minutes, "\n")
print("seconds:", seconds, "\n")

Begin learning Python:

References:

gorandp
  • 500
  • 5
  • 11