TypeError:'dict_keyiterator'对象不可订阅

时间:2017-09-26 23:08:51

标签: 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,我尝试使用带有此行代码的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

帖子的功能

@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文件中发送任何文件 火花信息错误

 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文件 任何帮助,谢谢所有

1 个答案:

答案 0 :(得分:4)

TL; DR您正在使用Python 3,而教程使用Python 2.您可以尝试:

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