def read_dict(file_name): f=open(file_name,'r') dict_rap={} for key, val in csv.reader(f): dict_rap[key]=str(val) f.close() return(dict_rap) test_dict = {'wassup':['Hi','Hello'],'get up through':['to leave','to exit'], 'its on with you':['good bye','have a nice day'],'bet':['ok','alright'],'ight':['ok','yes'], 'whip':['car','vechile'],'lit':['fun','festive'],'guap':['money','currency'],'finesse':['to get desired results by anymeans','to trick someone'], 'jugg':['how you makemoney','modern term for hustle'],'1111':['www'] }
Traceback (most recent call last): File "C:\Users\C2C\Desktop\rosetta_stone.py", line 97, in reformed_dict = read_dict(file_name)#,test_dict) File "C:\Users\C2C\Desktop\rosetta_stone.py", line 63, in read_dict for key, val in csv.reader(f): ValueError: too many values to unpack (expected 2)
答案 0 :(得分:0)
我担心csv.reader(f)
没有返回你期望它返回的内容。我不确切知道你的.csv文件是怎么样的,但我怀疑它会直接返回你试图放入字典的两个值。
假设.csv的前3行看起来像这样:
wassup,hi,hello
get up through,to leave,to exit
its on you,good bye,have a nice day
获取.cvs并迭代每一行的更好方法可能是:
...
my_csv = csv.reader(f)
for row in my_csv:
# row is a list with all the values you have in one line in the .csv
if len(row) > 1:
key = row[0] # for the 1st line the value is the string: 'wassup'
values = row[1:] # here for the first line you get the list: ['hi', 'hello']
# ... and so on
答案 1 :(得分:0)
来自var loginPageView = Backbone.View.extend({
events :{
"click #login" : "login",
"change input[type=radio]":"changeLanguage"
},
initialize : function() {
//Some Code
},
render: function() {
this.template = _.template(loginTpl);
$(this.el).html(this.template);
return this;
},
login:function(){
console.log("Login Clicked");
router.navigate('home', {trigger: false, replace: false});
},
changeLanguage:function(){
//console.log("change lang");
if(i18n.currentLocal == 'en'){
i18n.currentLocal='fr';
this.render();
$("#month2").attr('checked',true);
}else
{
if(i18n.currentLocal == 'fr'){
i18n.currentLocal='en';
this.render();
}
}
}
});
return loginPageView;
文档......
csv
我想这很自我解释......
我认为该列表的每一行都是您期待的字典。因此,您的In [2]: csv.reader??
Docstring:
csv_reader = reader(iterable [, dialect='excel']
[optional keyword args])
for row in csv_reader:
process(row)
......
......
The returned object is an iterator. Each iteration returns a row
处理代码应该进入迭代,迭代将遍历dict
答案 2 :(得分:0)
据说csv.reader(f)只会产生一件你想要处理的东西(key和val)。
假设您使用的是标准csv模块,那么您只能获得一个项目的列表。如果您希望输入有两个项目,那么您可能需要使用不同的分隔符。例如,如果您的输入具有半冒号而不是逗号:
csv.reader(f, delimiter=";")