我正在读取文本文件,并以list of dict
的形式将每一行写入两个文件中的一个。在我检查的最后,pickle包含dict的列表,但每个dict项都是相同的,这是最后插入的项
这里的逻辑是,如果选择1,则添加谣言列表,2则添加谣言列表,3则中断
此代码中的错误是什么:
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
您正在将相同的字典添加到每个列表中,只是在每次迭代中更改它以前的内容。试试这个:
而且,看起来您也在pickle转储每个迭代中的结果文件。你只需要做一次,在循环之后
相关问题 更多 >
编程相关推荐