[英]How to extract the time from one column to different separate column in hours, minute and second
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?如何在 Python 中以小时、分钟和秒为单位将时间从一列提取到不同的单独列?
Is that what you want?那是你要的吗?
Your "dat" file:您的“dat”文件:
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: Output:
20 31 47
23 33 46
10 20 00
11 30 00
12 15 00
Your text data are in a "dat" file.您的文本数据位于“dat”文件中。 You read the file line by line in a list using the readlines() method.
您可以使用 readlines() 方法在列表中逐行读取文件。
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).然后,您使用 re.sub 方法删除您的 header,该方法将字母替换为列表的连接产品(产生一个字符串)。 Finally, you replace the ":" by a space to print the output.
最后,将“:”替换为空格以打印 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.产生相同的 output。
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我不知道您使用的是什么数据,因为它的格式不正确,但如果它是一个字符串,您可以使用 .split 将每小时分钟和秒分隔成一个列表,然后您可以调用每个值
Crying_time = "20:31:47"
print(Crying_time.split(":"))
Would output会 output
['20', '31', '47']
And can be seen by using并且可以通过使用
print(Crying_time[0])
Would output会 output
'20'
If your numbers arent a string you can use str() to convert them to a string.如果您的数字不是字符串,您可以使用 str() 将它们转换为字符串。
x = 1234
x = str(x)
As I understand.我认为。 You have a file to read.
你有一个文件要读。 Let's name it
imsad.help
.我们将其命名为
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:然后,我知道你想分成 3 个向量:
So we can store these values in 3 different lists.所以我们可以将这些值存储在 3 个不同的列表中。 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:开始学习Python:
References:参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.