使用Python限制行数(CSV读取器)

使用Python限制行数(CSV读取器),python,python-3.x,list,row,csvreader,Python,Python 3.x,List,Row,Csvreader,我想在下面的python代码中给出行数限制,如果行数少于200行,则执行代码,如果行数超过200行,则不运行代码。 使用下面的代码,我正在打印行数,但是用于限制行数的if子句给了我错误 TypeError:view函数未返回有效响应。这个 函数返回None或结束时没有返回语句 错误:索引:在/CreateStudent[GET]上出现异常 我在浏览器中看到:服务器遇到内部错误 无法完成您的请求。要么服务器是 过载或应用程序中存在错误 @app.route('/CreateStudent',met

我想在下面的python代码中给出行数限制,如果行数少于200行,则执行代码,如果行数超过200行,则不运行代码。 使用下面的代码,我正在打印行数,但是用于限制行数的if子句给了我错误

TypeError:view函数未返回有效响应。这个 函数返回None或结束时没有返回语句

错误:索引:在/CreateStudent[GET]上出现异常

我在浏览器中看到:服务器遇到内部错误 无法完成您的请求。要么服务器是 过载或应用程序中存在错误

@app.route('/CreateStudent',methods=['GET','POST'])
def upload_student():
如果request.method==“POST”:
csv_file=request.files['file']
如果不是csv_文件:
返回渲染模板('error.html')
csv\u文件=文本IOWrapper(csv\u文件,编码为='utf-8')
csv\u reader=csv.reader(csv\u文件)
行=列表(csv\U读卡器)
打印(行)
如果长度(线)<200:
对于行中的行:
如果len(行)==4:
名称=行[0]
familyname=行[1]
年龄=第[2]行
作业=行[3]
创建_学生(姓名、家庭名称、年龄、工作)
时间。睡眠(2)
返回呈现模板('success.html')
返回呈现模板('CreateStudent.html')
当我还想打印行时,我看到的结果如下: ['Sara'、'Jacky'、'22'、'engineer']
为什么我的结果中有这个2[[],是因为列表吗?

在这里,我稍微修改了您的代码,并在修改的地方添加了注释:

if request.method == 'POST':
    csv_file = request.files['file']
    if not csv_file:
        return render_template('error.html')
    csv_file = TextIOWrapper(csv_file, encoding='utf-8')
    csv_reader = csv.reader(csv_file)
    lines = list(csv_reader)            # <--- read the file into `lines`

    if len(lines) < 200:
        for row in lines:               # <-- we're iterating over `lines` now
            if len(row)==4:
                create_Student(*row)    # <-- no need to extract to variables, simple `*` is enough
        return render_template('success.html')

    return render_template('CreateStudent.html')    # <-- this is returned in case len(lines) >= 200
if request.method==“POST”:
csv_file=request.files['file']
如果不是csv_文件:
返回渲染模板('error.html')
csv\u文件=文本IOWrapper(csv\u文件,编码为='utf-8')
csv\u reader=csv.reader(csv\u文件)

lines=list(csv_reader)#请包含实际的错误消息(例如
回溯
),否则如果没有原始数据和/或执行代码的上下文,其他人将很难看到问题所在。也就是说,如果
csv_reader
是迭代器,调用
list(csv\u reader)
会消耗每一行,因此
if
语句中的循环不会留下任何要消耗的内容。将
lines=len(列表(csv\u读取器))
更改为
read\u lines=list(csv\u读取器)
。然后
lines=len(读线)
。然后在“for”循环中迭代读取行而不是csv读取器。@Motatoster:错误代码已添加。@AdriankAver:我修改了代码并获得了我共享的错误。我认为这也与您告诉我要应用的更改类似。@andrajkesely:谢谢,我检查了您的代码,但遇到以下错误:raise TypeError(TypeError:view函数未返回有效响应。该函数要么未返回任何响应,要么在没有返回语句的情况下结束。我修改了我的代码,如果您能查看一下,我将不胜感激。此外,我发现此错误在修改后发生:“错误:索引:/CreateSubscription[GET]上的异常”@CodeGirl我更新了我的答案。在代码末尾添加了
return render_template('CreateStudent.html')
。@andekesely:非常感谢。已经成功完成了。
if request.method == 'POST':
    csv_file = request.files['file']
    if not csv_file:
        return render_template('error.html')
    csv_file = TextIOWrapper(csv_file, encoding='utf-8')
    csv_reader = csv.reader(csv_file)
    lines = list(csv_reader)            # <--- read the file into `lines`

    if len(lines) < 200:
        for row in lines:               # <-- we're iterating over `lines` now
            if len(row)==4:
                create_Student(*row)    # <-- no need to extract to variables, simple `*` is enough
        return render_template('success.html')

    return render_template('CreateStudent.html')    # <-- this is returned in case len(lines) >= 200