我正在上传一个csv文件,并希望循环访问数据并将每个单元格插入数据库。这是python代码:
import csv
@app.route("/uploadcsv", methods=['POST'])
def uploadcsv():
myfile = request.files['file']
r = csv.reader(myfile)
headers = r.next()
for row in r:
print str(row[0])
print row[1]
print row[2]
print row[3]
print row[4]
print row[5]
print row[6]
print row[7]
print row[8]
# put into database
return "OK"
csv文件中当前有3行(稍后会有更多行)并且只打印第一行,如何打印所有行?
csv文件是:
first_name,last_name,email,phone,designation,company,industry,tag,created_at
john,smith,john@example.com,1234567,some company,some industry,some tag, now()
答案 0 :(得分:2)
将return "OK"
语句的缩进减少一级 - 就像它当前一样,一旦打印完第一行就从uploadcsv()
返回,而不是按照你的意图在for之后返回-loop。