使用 Python (CSV Reader) 限制行号
Limiting row numbers with Python (CSV Reader)
我想在下面的 python 代码中限制行数,如果行数少于 200 行则执行代码,如果行数多于 200 则 nit 运行 代码。
使用下面的代码,我正在打印行数,但是用于限制行的 if 子句给我错误。
TypeError: The view function did not return a valid response. The
function either returned None or ended without a return statement.
ERROR:index:Exception on /CreateStudent[GET]
What i see in the browser: The server encountered an internal error
and was unable to complete your request. Either the server is
overloaded or there is an error in the application.
@app.route('/CreateStudent', methods=['GET','POST'])
def upload_student():
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)
print(lines)
if len(lines) < 200:
for row in lines:
if len(row)==4:
name=row[0]
familyname=row[1]
age=row[2]
job=row[3]
create_student(name,familyname,age,job)
time.sleep(2)
return render_template('success.html')
return render_template('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
我想在下面的 python 代码中限制行数,如果行数少于 200 行则执行代码,如果行数多于 200 则 nit 运行 代码。 使用下面的代码,我正在打印行数,但是用于限制行的 if 子句给我错误。
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
ERROR:index:Exception on /CreateStudent[GET]
What i see in the browser: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
@app.route('/CreateStudent', methods=['GET','POST'])
def upload_student():
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)
print(lines)
if len(lines) < 200:
for row in lines:
if len(row)==4:
name=row[0]
familyname=row[1]
age=row[2]
job=row[3]
create_student(name,familyname,age,job)
time.sleep(2)
return render_template('success.html')
return render_template('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