I have a text file that reads. I managed to get everything sorted but what I don't understand is that my code did not get executed in the else statement. (The Else statement will skip the useless data and will not add to the PriorityQueue.)
id_is_g0000515
num_is_0.92
id_is_g0000774
uselessdata2
num_is_1.04
hithere
id_is_g0000377
num_is_1.01
pt21
id_is_g0000521
num_is_5.6
import os, sys, shutil, re
def readFile():
from queue import PriorityQueue
str1 = 'ID_IS'
str2 = 'NUM_IS'
q = PriorityQueue()
#try block will execute if the text file is found
try:
fileName= open("Real_format.txt",'r')
for line in fileName:
for string in line.strip().split(','):
if string.find(str1): #if str1 is found
q.put(string[-4:]) #store it in PQ
elif string.find(str2):#if str2 is found
q.put(string[-8:]) #store it in PQ
else:
line.skip() #skip the useless string
#q.remove(string)
fileName.close() #close the file after reading
print("Displaying Sorted Data")
#print("ID TYPE Type")
while not q.empty():
print(q.get())
#catch block will execute if no text file is found
except IOError:
print("Error: FileNotFoundException")
return
readFile()
Aucun commentaire:
Enregistrer un commentaire