I am reading text file and writing each line to one of two file in form of list of dict
. At the end when I check, pickle contains list of dict. But each dict item is same, and that is last inserted item.
Here logic is, if choice is 1, add in rumour list, 2 then add in norumour list, 3 then break.
What is the bug in this code:
fp = open('130615.txt', 'r')
count1 = 1
count2 = 1
result = {}
final_result_rumour = []
final_result_norumour = []
for val in fp.readlines():
temp = val.split('|')
try:
result['script_code'] = temp[0]
result['company'] = temp[1]
result['subject'] = temp[2]
result['date'] = temp[3]
result['link'] = temp[4]
result['description'] = get_description(result['link'])
if 'merge' in temp[2] or 'buy' in temp[2] or 'sale' in temp[2] or 'tie-up' in temp[2] or 'tie' in temp[2] or 'acquire' in temp[2] or 'amalgamation' in temp[2] or 'purchase' in temp[2] or 'amalgamate' in temp[2] or 'acquisition' in temp[2]:
f1 = open('suspected.txt','a')
print temp[2]
flag = raw_input("Enter your choice - ")
if flag == '1':
#1.write(temp[2]+'\n')
print "Rumour suspected : ", count1
count1 += 1
final_result_rumour.append(result)
output1 = open('rumours.pkl', 'wb')
pickle.dump(final_result_rumour, output1)
output1.close()
elif flag == '2':
print "No Rumour suspected : ", count2
count2 += 1
final_result_norumour.append(result)
output2 = open('norumours.pkl', 'wb')
pickle.dump(final_result_norumour, output2)
output2.close()
elif flag == '3':
confirm = raw_input('You want to proceed ? ')
if confirm == '1':
break
else:
print "No Rumour suspected : ", count2
count2 += 1
final_result_norumour.append(result)
output2 = open('norumours.pkl', 'wb')
pickle.dump(final_result_norumour, output2)
output2.close()
except:
pass