简体   繁体   English

将琴弦分成两部分

[英]Split the strings in two parts

I need some help with my code, I am writing the code as I want to create the variable to get the strings that will converts the time from 24 hours to 12 hours. 我的代码需要一些帮助,我正在编写代码,因为我想创建变量以获取将时间从24小时转换为12小时的字符串。 Then I want to split the strings to get the minute after the : . 然后我要分割字符串以得到:之后的分钟。

Example: 例:

When my strings shows as: 当我的字符串显示为:

22:30
22:30
22:40
23:05
23:00
23:00
23:00

Output: 输出:

10:30 PM
10:30 PM
10:40 PM
11:05 PM
11:00 PM
11:00 PM
11:00 PM

So when I try this: 所以当我尝试这个:

stop_time = str(stop_date[1])

converts_time = time.strptime(stop_time, "%H:%M")
converts_time = time.strftime("%I").lstrip('0') + str(":") + str(stop_time.split(":")) + time.strftime("%p")

I will get this: 我会得到这个:

10:['22', '30'] PM
10:['22', '30'] PM
10:['22', '40'] PM
10:['23', '05'] PM
10:['23', '00'] PM
10:['23', '00'] PM
10:['23', '00'] PM

I want to use the lstrip method to remove the 0 when my current time show in PM and I don't wish to use the lstrip when my current time show in AM. 我要使用lstrip方法将当前时间显示在PM时将0删除,而我不希望将我的当前时间显示在AM时使用lstrip。 As you can see that I am using stop_time.split method, I want to converts the hours from 24 to 12 hours then I want to split the strings after the : to get the minute for each string to make it show like: 10:30 PM, 10:40 PM...etc so I can use that variable to compare with the other variable to see if the time is less than. 如您所见,我正在使用stop_time.split方法,我想将小时数从24小时转换为12小时,然后我想在:之后拆分字符串,以获取每个字符串的分钟数,使其显示为:10:30 PM,10:40 PM ...等,因此我可以使用该变量与另一个变量进行比较,以查看时间是否小于。

Can you please show me an example how I can converts the time from 24 hours to 12 hours, then split the strings after the : to get the minute with the AM or PM ? 您能否给我一个示例,说明如何将时间从24小时转换为12小时,然后在:之后分割字符串以获取AMPM的分钟数?

Your problem comes from the end of your last line. 您的问题来自最后一行的末尾。 When you use split it is returning a list of two strings. 使用split时,它将返回两个字符串的列表。 Since you are only interested in the second (index 1) you can specify this by adding [1] after the split function. 由于您只对第二个(索引1)感兴趣,因此可以通过在split函数后面添加[1]来指定它。

converts_time = time.strftime("%I").lstrip('0') + str(":") + str(stop_time.split(":")[0]) + time.strftime("%p")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM