如何以CSV文件格式获取输出?

时间:2019-06-25 17:37:03

标签: python pandas csv printing output

我正在以这种格式输出。有什么办法可以将输出保存为CSV格式?

print("Source rule: {} \t similarity with Target Rule is \n {}\n".format(rule, sim_scores))
    print("\n")

输出:

Source rule: 2.1 Separation of trains    similarity with Target Rule is 
 [{'Cosine Sim Score': 1.0, 'Target Rule': '2.1 Separation of Trains'}]



Source rule: 2.1.1 Context   similarity with Target Rule is 
 [{'Cosine Sim Score': 1.0000001, 'Target Rule': '2.1.1 - Context'}, {'Cosine Sim Score': 1.0000001, 'Target Rule': '2.2.1 Context'}, {'Cosine Sim Score': 1.0000001, 'Target Rule': '2.3.1 Context'}, {'Cosine Sim Score': 1.0000001, 'Target Rule': '2.4.1 Context'}, {'Cosine Sim Score': 1.0000001, 'Target Rule': '2.5.1 Context'}, {'Cosine Sim Score': 1.0000001, 'Target Rule': '2.6.1 Context'}, {'Cosine Sim Score': 1.0000001, 'Target Rule': '2.7.1 Context'}, {'Cosine Sim Score': 1.0000001, 'Target Rule': '2.8.1 Context'}, {'Cosine Sim Score': 1.0000001, 'Target Rule': '2.9.1 Context'}]

1 个答案:

答案 0 :(得分:0)

如果您尝试输出到csv,excel或html文件,则可以使用“ to_csv”方法。参见示例

result = pd.read_csv('filename')

result.to_csv('result.csv')
result.to_excel('result.xlsx')
result.to_html('result.htm')