在Python 3中,我试图计算该csv文件中的分级数量,将其拉入列表中,但是当我尝试实际开始操作列表中的数据时,我也得到了同样的错误在我的for循环中有很多要解压缩的值(预期为5),我不知道为什么。
public IList<PersonAgeGroup> GroupPersonsByAge(List<Person> persons) {
return (from person in persons
group person by person.Age into ages
select new PersonAgeGroup { Age = ages.Key, Persons = ages.ToList() }
).ToList();
}
当我尝试运行此命令时,出现以下错误:
with open(r'C:\Users\t_goo\Pictures\coding\dataset\smartphone-
review\amazon_alexa.csv', mode='r') as alexa:
data = list(alexa.read().split(","))
print(data)
# rating, date, variation, review, feedback
for rating, data, variation, review, feedback in data:
rating = rating.count()
print(rating)
以下是原始数据的示例:
line 7, in <module> for rating, data, variation, review, feedback in data: ValueError: too many values to unpack (expected 5)
答案 0 :(得分:1)
data = list(alexa.read().split(","))
一次读取整个文件,然后在,
处将其分割-您需要先将文件分割成几行,然后在逗号前每行分割 ,所以您需要这样做
data = [y.strip().split(",") for y in alexa.splitlines() if y.strip()]
这将产生一个列表列表。
然后您可以使用
for rating, data, variation, review, feedback in data:
pass
对其进行操作