python - 类型错误 : 'dict_keyiterator' object is not subscriptable

标签 python python-3.x apache-spark curl pyspark

我尝试在这里学习这个教程 https://www.codementor.io/jadianes/building-a-web-service-with-apache-spark-flask-example-app-part2-du1083854

使用spark,flask,我尝试使用cmd和这行代码在win 10中使用curl发送文件user_ ratings.file

curl --data-binary @user_ratings.file http://127.0.0.1:5432/0/ratings

该文件没有格式,只有此数据

260,9 1,8 16,7 25,8 32,9 335,4 379,3 296,7 858,10 50,8

post的函数是

@main.route("/<int:user_id>/ratings", methods=["POST"])
def add_ratings(user_id):
    # get the ratings from the Flask POST request object
    ratings_list = request.form.keys()[0].strip().split("\n")
    ratings_list = map(lambda x: x.split(","), ratings_list)
    # create a list with the format required by the negine (user_id, movie_id, rating)
    ratings = map(lambda x: (user_id, int(x[0]), float(x[1])), ratings_list)
    # add them to the model using then engine API
    recommendation_engine.add_ratings(ratings)

    return json.dumps(ratings)

但它不会发送 csv 文件中的任何内容 Spark 消息错误为

 File "C:\Users\ibrahim\Desktop\RSS\app.py", line 33, in add_ratings
    ratings_list = request.form.keys()[0].strip().split("\n")
TypeError: 'dict_keyiterator' object is not subscriptable
127.0.0.1 - - [26/Sep/2017:23:46:48 +0200] "POST /0/ratings HTTP/1.1" 500 291 "-" "curl/7.55.1"

我似乎函数中的这一行

 ratings_list = request.form.keys()[0].strip().split("\n")

在教程中它将数据发布到 csv 文件 任何帮助,谢谢大家

最佳答案

TL;DR 您使用的是 Python 3,而教程使用的是 Python 2。您可以尝试:

ratings_list = list(request.form.keys())[0].strip().split("\n")

关于python - 类型错误 : 'dict_keyiterator' object is not subscriptable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46437095/

相关文章:

json - 使用 Spark 解析多个 JSON 模式

python - 无要求的Elasticsearch模糊查询

java - Apache Spark MLlib : OLS regression in Java

python - 在 python 中实现有向图

python-3.x - 有没有办法从其他键中的键中获取值?

python - 如何在列表中构建压缩 for 循环

python - 如果在列表中找不到 key ,如何获得默认值零?

apache-spark - 在多列上应用窗口函数

python - 查找一段时间内每个项目和 ID 的平均值 (Python)

python - 将变量传递给函数时如何使用python timeit?