比较字符串中的int

时间:2016-02-14 21:54:18

标签: python python-2.7

我正在处理我的代码,因为我想比较if语句中的字符串中的int,以查看该值是否等于或大于。

当我尝试这个时:

for prog_clock, prog_length, pos_X, prog_ids in izip_longest(program_clock, programs_length, posX, progId, fillvalue=''):
    epg_time_1 = self.getControl(344).getLabel()
    epg_time_2 = self.getControl(345).getLabel()
    epg_time_3 = self.getControl(346).getLabel()


    if int(pos_X) == 375:
       for program_id, program_length, program_minutes in zip(prog_id_list, programs_length, program_remaining):

           if epg_time_2 == prog_clock:
              print "WORKING 1"

           elif prog_clock > epg_time_1 and epg_time_2 > prog_clock:
              print "WORKING 2"


           elif epg_time_3 == prog_clock:
              print "WORKING 3"


           elif epg_time_3 < prog_clock:
               print "WORKING 4"

prog_clock的输出:

21:41:45 T:5796  NOTICE: 11:00PM
21:41:45 T:5796  NOTICE: 10:00PM
21:41:45 T:5796  NOTICE: 10:00PM
21:41:45 T:5796  NOTICE: 10:45PM
21:41:45 T:5796  NOTICE: 11:05PM
21:41:45 T:5796  NOTICE: 10:00PM
21:41:45 T:5796  NOTICE: 10:00PM

epg_time_1的输出:

9:30PM

epg_time_2的输出:

10:00PM

epg_time_3的输出:

10:30PM

我想比较来自对象字符串的三个字符串和每个prog_clock字符串,以查看我可以为电视节目的每个字符串传递哪个if语句。当我尝试比较它们时,它不会让我传递任何这些if语句。那么我如何比较字符串中的int以查看该值是等于还是大于?

3 个答案:

答案 0 :(得分:0)

好的,对于每一行,您可以从下面应用函数parseLine(str)。我正在做的基本上是以下几点:

对于给定的行(例如:21:41:45 T:5796 NOTICE: 10:00PM)I

  1. 获取第一个时间戳,我们知道格式为HH:MM:SS
  2. 获取第二个时间戳,我们知道格式为HH:MM(AM/PM)
  3. 将两者都转换为时间对象(请参阅here
  4. 比较
  5. 当然,您可以根据行的格式调整格式(如果您决定更改它)。重要的是正确转换时间戳,因为第一个使用24小时格式,但第二个 - 12小时格式(我使用%I将第二个时间戳的小时部分转换为24小时格式这样我们就可以轻松地比较两者了。比较本身由Python和支持它的时间对象完成。此外,我假设您的两个时间戳都使用空格作为中间子字符串的分隔符,但如果第一个时间戳由制表符分隔,您可以将split()的分隔符选项从' '更改为{ {1}}。

    '\t'

    实际上有多种解决方案,这可能不是最好的解决方案,但它可以解决问题。我最后做的测试结果是:

    from datetime import datetime
    
    def parseLine(line):
      # Use split() and rsplit() to get the stamps
      # Extract the first timestamp - it is separated from the rest by the first space in your string
      first_tstamp = line.split(' ', 1)[0]
      # Extract the second timestamp - it is separated from the rest by the last space in your string
      last_tstamp = line.rsplit(' ', 1)[1]
    
      # Parse the string representations of both stamps to a time object
      t_first = datetime.strptime(first_tstamp, "%H:%M:%S").time()  # Format: HH:MM:SS
      t_second = datetime.strptime(last_tstamp, "%I:%M%p").time() # Format: HH:MM(PM/AM)
    
      print("FIRST",t_first)
      print("SECOND",t_second)
    
      if(t_first > t_second): return 1
      elif(t_first < t_second): return -1
      else: return 0
    
    def test(line):
      res = parseLine(line)
    
      if(res > 0): print("FIRST later than LAST")
      elif(res < 0): print("FIRST earlier than LAST")
      else: print("FIRST same as LAST")
    
    print("--------Test 1--------")
    # First is earlier than second  
    test("21:41:45 T:5796 NOTICE: 11:00PM")
    print("--------Test 2--------")
    # First is later than second
    test("12:05:30 T:5796 NOTICE: 10:00AM")
    print("--------Test 3--------")
    # First is same as second
    test("23:00:00 T:5796 NOTICE: 11:00PM")
    

答案 1 :(得分:0)

对于prog_clock从字符串中提取时间组件,使用datetime.strptime()解析它并将其转换为datetime.time对象。现在你可以直接比较时间。只需添加四行来解析输入,如下所示:

from datetime import datetime

for prog_clock, prog_length, pos_X, prog_ids in izip_longest(program_clock, programs_length, posX, progId, fillvalue=''):
    epg_time_1 = self.getControl(344).getLabel()
    epg_time_2 = self.getControl(345).getLabel()
    epg_time_3 = self.getControl(346).getLabel()

    # parse time data into datetime.time objects for comparison
    prog_clock = datetime.strptime(prog_clock.split()[-1], '%I:%M%p').time()
    epg_time_1 = datetime.strptime(epg_time_1, '%I:%M%p').time()
    epg_time_2 = datetime.strptime(epg_time_2, '%I:%M%p').time()
    epg_time_2 = datetime.strptime(epg_time_3, '%I:%M%p').time()

    ...

实际上,转换为datetime.time个对象是可选的 - 如果省略它,datetime.datetime值将具有相同的日期(1900-01-01),并且时间比较仍将有效。

答案 2 :(得分:-2)

如果字符串相同,则直接比较它们

string1 = '10:00PM'
string2 = '11:00PM'

print(string2 > string1)

print(string1 > string2)

输出

True
False

编辑:阅读评论后,我决定编辑我的答案并发布time.strptime

的解决方案
from time import strptime

format = '%I:%M%p' # format to striptime

epg_time_1 = strptime('9:30PM', format)
epg_time_2 = strptime('10:10PM', format)
epg_time_3 = strptime('10:30PM', format)


prog_clock = strptime('10:00PM', format)

if epg_time_2 == prog_clock:
    print("WORKING 1")

elif prog_clock > epg_time_1 and epg_time_2 > prog_clock:
    print("WORKING 2")


elif epg_time_3 == prog_clock:
    print("WORKING 3")


elif epg_time_3 < prog_clock:
    print("WORKING 4")

输出"WORKING 2"