Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 迭代列表副本时的异常行为_Python_List_Python 2.7_Dictionary_Flask - Fatal编程技术网

Python 迭代列表副本时的异常行为

Python 迭代列表副本时的异常行为,python,list,python-2.7,dictionary,flask,Python,List,Python 2.7,Dictionary,Flask,示例:通过测试使用Flask和Jinja在模板中打印 @app.route('/mypage') def mypage(): # just open the json file and read it... input_file = file(FILE, "r") j = json.loads(input_file.read().decode("utf-8-sig")) for item in j[:]: test = "

示例:通过
测试
使用Flask和Jinja在模板中打印

@app.route('/mypage')
def mypage():        
    # just open the json file and read it...    
    input_file  = file(FILE, "r")
    j = json.loads(input_file.read().decode("utf-8-sig"))
    for item in j[:]:
        test = "test"       

    return render_template('mypage.html', j=j, test = test, config = {'environment_args':{'autoescape':False}})
注意
j[:]

现在,当我想在HTML文件中打印它时:

<p>{{test}}</p>
更新:这是json文件的一个示例:

[
    {
        "trailers": {
            "quicktime": [], 
            "youtube": [
                {
                    "source": "FZCxSiYRh7Q", 
                    "type": "Trailer", 
                    "name": "Trailer 1", 
                    "size": "Standard"
                }
            ], 
            "id": 54315
        }, 
        "date": "2008", 
        "genres": [
            {
                "id": 18, 
                "name": "Drame"
            }
        ], 
        "tagline": "", 
        "popularity": 0.23, 
        "vote_count": 2, 
        "start": "Monday 09 December 01:10", 
        "length": "95", 
        "overview": "", 
        "vote_average": 10.0, 
    }, 

    {etc}
]

您显示的JSON似乎有一个错误——最后一个数字后面的逗号。下面是一个适合我的例子:

#app.py
from flask import Flask, render_template, json

app = Flask(__name__)


@app.route('/')
def mypage():
    # just open the json file and read it...
    input_file = file('file.json', "r")
    j = json.loads(input_file.read().decode("utf-8-sig"))
    for item in j[:]:
        print item
        test = "test"

    return render_template('index.html', j=j, test=test, config={'environment_args': {'autoescape': False}})


if __name__ == '__main__':
    app.run(debug=True)
一个示例HTML模板,呈现您的测试变量,并遍历JSON结构以打印每个开始日期:

<!-- index.html -->
<html>
    <body>
        {{ test }}
        {% for row in j %}
            <li>{{ row.start }}</li>
        {% endfor %}
    </body>
</html>

你能提供一个JSON片段的例子让你重现这个错误吗?@SeanVieira I添加了一个例子:-)@4m1nh4j1在这个循环中,
test='test'
应该做什么?我想,如果您直接通过
test='test'
到模板,它会起作用吗?否则,如果
j
没有元素,我会期望出现
namererror
。。。很奇怪为什么你会得到
None
。。。“迭代列表副本”在这里是一个麻烦事,我用来从
j
读取元素,创建其他元素,如
var=int(item['vote\u average'])
,然后
返回呈现模板(“page.html”,j=j,var=var)
<!-- index.html -->
<html>
    <body>
        {{ test }}
        {% for row in j %}
            <li>{{ row.start }}</li>
        {% endfor %}
    </body>
</html>
[
    {
        "trailers": {
            "quicktime": [],
            "youtube": [
                {
                    "source": "FZCxSiYRh7Q",
                    "type": "Trailer",
                    "name": "Trailer 1",
                    "size": "Standard"
                }
            ],
            "id": 54315
        },
        "date": "2008",
        "genres": [
            {
                "id": 18,
                "name": "Drame"
            }
        ],
        "tagline": "",
        "popularity": 0.23,
        "vote_count": 2,
        "start": "Monday 09 December 01:10",
        "length": "95",
        "overview": "",
        "vote_average": 10.0
    }
]