我有一个这样的csvFile:
id;verbatim
1;je veux manger
2;tu as manger
我有我的脚本,该脚本返回这样的字典:
dico_phrases = {"['manger']": ['7', '1', '0'], "['être', 'laid']": ['0', '21', '1041']}
T想添加4个新闻栏,如下所示:
id;verbatim,key,value1,value2,value3
1;je veux manger
2;tu as manger
然后将我的词典添加到这样的每列中:
id;verbatim;key,value1,value2,value3
1je veux manger;manger;7;1;0
2;tu as manger;être laid;0;21;1041
下面的脚本允许我获取字典:
with open('output.csv','wb') as f:
w = csv.writer(f)
w.writerow(dico_phrases.keys())
w.writerow(dico_phrases.values())
我有这个:
['manger'],"['être', 'laid']"
"['7', '1', '0']","['0', '21', '1041']"
这不是我想象的
答案 0 :(得分:0)
考虑为此使用pandas-
df = pd.read_csv("input.csv", index_col='id')
dico_phrases = {"['manger']": ['7', '1', '0'], "['être', 'laid']": ['0', '21', '1041']}
df['key'] = [" ".join(eval(x)) for x in dico_phrases.keys()]
df = df.join(pd.DataFrame([x for x in dico_phrases.values()], index=df.index))
df.to_csv("output.csv")
输出:
id,verbatim,key,0,1,2
1,je veux manger,manger,7,1,0
2,tu as manger,être laid,0,21,1041