【发布时间】:2016-04-14 14:11:08
【问题描述】:
我有一个包含
的文本文件id_is_g0000515
num_is_0.92
id_is_g0000774
num_is_1.04
id_is_g0000377
num_is_1.01
id_is_g0000521
num_is_5.6
假设按“g0000515”和仅按数字“0.92”对数据进行排序,没有字符串“id_is_”和“num_is_”。它给了我一个错误 TypeError: 'method' object is not subscriptable。有人能帮我吗?
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()
【问题讨论】:
-
请包含异常的full traceback,这样我们就不用猜测发生在哪里了。听起来好像您正在尝试使用具有
[...]订阅访问权限的方法,而不是使用(...)调用该方法。 -
您希望
q.put[-4:]做什么?您正在使用PriorityQueue.put()方法,就好像它是那里的列表一样,这是行不通的。 -
回溯(最近一次调用最后):文件“F:/Project Python/SortingAlgorithmBetaV2.py”,第 32 行,在
readFile() 文件“F:/Project Python/SortingAlgorithmBetaV2.py”中",第 15 行,在 readFile q.put[-4:] TypeError: 'method' object is not subscriptable -
请edit您的问题添加更多类似的信息。
-
q.put[-4:] 是提取“id_is_g0000515”或“num_is_0.92”的子字符串,这样我只得到g0000515和0.92没有字符串
标签: python string sorting io priority-queue