python-3.x 如何修复'int'和'str'示例之间不支持'< '?[关闭]

n53p2ov0  于 2023-11-20  发布在  Python
关注(0)|答案(1)|浏览(115)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
两年前关闭。
Improve this question
我正在使用Python 3.6。我试图收集数据并更新流表。但current_data_flow[1]和current_data_flow[2]的IP地址是字符串。我收到错误,因为'int'和'str'的示例之间不支持'<'。我尝试了字符串类型转换,但它说'必须是str,而不是int'
下面是代码和错误。

class RawFlowProcessor(threading.Thread):
    def __init__(self, threadID):
        threading.Thread.__init__(self)
        self.threadID = threadID
    def run(self):
        while True:
            global raw_flow_data
            try:
                if not q_rawData.empty():
                    start_time = time.time()
                    current_flow_data = q_rawData.get()
                    current_flow_data = str(current_flow_data)
                    count = 0
                    for items in reversed(raw_flow_data):
                        if current_flow_data[0:5]==items[0:5]:
                        count = 1
                        #If Flow bytes and/or packets are being updated
                        if (current_flow_data[6]!=items[6] or current_flow_data[7]!=items[7]):
                            current_flow_data[8]=items[8] #Retain timestamp for the first time the flow was seen
                            if (current_flow_data[5]>FLOW_TIMEOUT): #If a Flow remains in Flow Table for more than FLOW_TIMEOUT
                                q_finalData.put(current_flow_data)
                            items[5:8] = current_flow_data[5:8] #Get the new values
                            #~ raw_flow_data.insert(idx, current_flow_data)
                            #~ raw_flow_data.remove(items)
                            #count = 1
                            break
                        #If Flow bytes and/or packets are not being updated and Flow is idle in the Flow Table
                        elif (current_flow_data[6]==items[6] or current_flow_data[7]==items[7]):
                            if (current_flow_data[5]!=0 or current_flow_data[6]!=0):
                                #count = 1
                                temp_value = items[5] + 3
                                if (current_flow_data[5]>temp_value): #To account for system delays
                                    q_finalData.put(items)
                                    break
                            elif (current_flow_data[5]==0 or current_flow_data[6]==0): #Flow not yet updated
                                #count = 1
                                break
                   if count==0: #New Flow Entry
                       raw_flow_data.append(current_flow_data)
                       raw_flow_data = sorted(raw_flow_data, key=itemgetter(8)) #Sort raw_flow_data according to last_seen
                   end_time = time.time() - start_time
                   thread_two.append(end_time)

           except Exception as ex:
           template = "An exception of type {0} occured. Arguments:\n{1!r}"
           message = template.format(type(ex).__name__, ex.args)
           print(message)
           print('\nRaw Flow Processor shows an error!!')
           break

字符串
这是错误消息,我似乎无法弄清楚。

An exception of type TypeError occured. Arguments: (“'<' not supported between instances of 'int' and 'str'”,)


在尝试字符串类型转换错误消息后,

An exception of type TypeError occured. Arguments:
('must be str, not int',)

Raw Flow Processor shows an error!!

58wvjzkj

58wvjzkj1#

这个错误消息非常说明问题,虽然“can 't compare int and str with >"。好的!所以我们需要看看我们的比较并思考我们的逻辑。你在评论中说它们是IP地址,那么.你是如何比较它们的?
“10.10.10.10“是否> 42?我不知道.

if (current_flow_data[5]>FLOW_TIMEOUT):
  q_finalData.put(current_flow_data)

字符串

def check_timeout(flow_data):
  return flow_data.timeout > FLOW_TIMEOUT

if check_timeout(current_flow_data):
  q_finalData.put(current_flow_data)


访问timeout比访问[6]更容易理解,并且阅读方法的名称比内联所有逻辑更容易理解(对于如此短的内容,可能没有太大帮助)

相关问题