1.python之写.csv文件
import pandas as pd
df_dict={'a':2,'b':6,'c':7}
df_word = pd.Dataframe(df_dict.keys(),columns=['word'])
df_weight = pd.Dataframe(df_dict.values(),columns=['weight'])
word_weight = pd.concat([df_word, df_weight], axis=1) # 拼接词汇列表和权重列表
word_weight = word_weight.sort_values(by="weight",ascending = False) # 按照weight列值降序排列
str='%s%s%s' % ("result/", 'aaa',".csv") # 数据存储于”result/aaa.csv“
#写入.csv文件
word_weight.to_csv(str,index=False,encoding="GBK")
2.python之读.csv文件
#读取.csv文件
data = pd.read_csv(r'result/aaa.csv',sep=',')
for index,row in data.iteritems():
print(index,row[0],row[1],row[2])
输出结果:
word c b a
weight 7 6 2