Python 与'有关的问题;方法';对象不可下标

Python 与'有关的问题;方法';对象不可下标,python,string,sorting,io,priority-queue,Python,String,Sorting,Io,Priority Queue,我有一个文本文件,其中包含 id_为_g0000515 num_为0.92 id_为_g0000774 数值为1.04 id_为_g0000377 数值为1.01 id_为_g0000521 数值为5.6 它假定按“g0000515”和仅按数字“0.92”对数据进行排序,而不包含字符串“id_is_”和“num_is_”。它给我一个错误TypeError:“method”对象不可下标。有人能帮我吗 import os, sys, shutil, re def readFile():

我有一个文本文件,其中包含

id_为_g0000515

num_为0.92

id_为_g0000774

数值为1.04

id_为_g0000377

数值为1.01

id_为_g0000521

数值为5.6

它假定按“g0000515”和仅按数字“0.92”对数据进行排序,而不包含字符串“id_is_”和“num_is_”。它给我一个错误TypeError:“method”对象不可下标。有人能帮我吗

    import os, sys, shutil, re


def readFile():
    from queue import PriorityQueue
    q = PriorityQueue()
    #try block will execute if the text file is found
    try:
        fileName= open("Real_format.txt",'r')
        #for tuple in fileName:
            #fileName.write('%s',tuple)
        for line in fileName:
                for string in line.strip().split(','):
                    if string.find("id_is_"):
                        q.put[-4:] #read the ID only as g0000774
                    elif string.find("num_is_"):
                        q.put[-4:] #read the num only as 0.92

        fileName.close() #close the file after reading          
        print("Displaying Sorted Data")
        #print("ID TYPE       Type")
        while not q.empty():
            print(string[30:35] + ":     " +q.get())
            #print(q.get())

            #catch block will execute if no text file is found
    except IOError:
                print("Error: FileNotFoundException")
                return

readFile() 

您正在尝试在此处切片
PriorityQueue.put()
方法:

q.put[-4:]
那是行不通的;方法对象不可切片。我认为您希望将
字符串
变量切片,并将除前4个字符以外的所有字符放入队列:

q.put(string[4:])
注意,我在这里使用了一个正数;您不想要列表中的4个字符,您想要的是除前4个字符以外的所有字符

当字符串以
“num_is_u”
开头时,需要跳过更多字符<代码>数量为7个字符,而不是4个:

q.put(string[7:])

请包含异常的完整回溯,这样我们就不必猜测在哪里发生了这种情况。听起来好像您正在尝试使用具有
[…]
订阅访问权限的方法,而不是使用
(…)
调用该方法。您希望
q.put[-4:]
做什么?您正在使用
PriorityQueue.put()
方法,就好像它是一个列表一样,这是行不通的。回溯(最近一次调用):文件“F:/Project Python/SortingAlgorithmBetaV2.py”,第32行,在readFile()文件“F:/Project Python/SortingAlgorithmBetaV2.py”的第15行,在readFile q.put[-4:]TypeError:“method”对象不可下标请您的问题添加更多类似的信息。q.put[-4:]是提取“id_is_g0000515”或“num_is_0.92”的子字符串,这样我只得到g0000515和0.92,而不带字符串