简体   繁体   English

如何以小时、分钟和秒为单位将时间从一列提取到不同的单独列

[英]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 个向量:

  • hours小时
  • minutes分钟
  • seconds

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.

相关问题 从单个列中为日期,月,年,小时,分钟,秒分别创建列 - Make separate columns for Date, Month, year, hour , minute , second from a single column 如何从 pandas 数据帧中的时间戳列中提取小时数并转换为浮点类型 - How to extract hours from time stamp column in pandas data frame and convert to float type 如何从一列中提取值并根据 python 中的目标列创建单独的二进制列 - How to extract values from one column and create separate binary columns based on the targeted column in python 如何在 python 中将日期时间列重置为以一分钟为增量 - How to reset a date time column to be in increments of one minute in python 如何将一列中基于不同类别的数据行提取到单独的文本文件中? - How can I extract lines of data based on different categories in one column into separate text files? 如何从timedelta对象的DataFrame / Series列中提取小时? - How to extract hours from DataFrame/Series column of timedelta objects? 如何从填充了 datetime.time 值的系列中提取小时、分钟和秒 - How to extract hour, minute and second from Series filled with datetime.time values 如何比较另一列数据框中的一个可用列值是否可用以及如何提取第二个数据帧中的另一列(如果存在) - How to compare one column value available or not in another column dataframe and extract another column of second dataframe if present 如何从 Python 中的列中的 0 天 09:30:15 之类的时间戳列中提取小时和小步舞曲 - How to extract Hours and Minuets from a column of timestamp like 0 Days 09:30:15 from column in Python 在字典中制作字典以通过一列中的相同值分隔数据,然后从第二列中分隔数据 - Making dictionary in dictionary to separate data by the same values in one column and then from second column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM