小编Jed*_*art的帖子

如何更新CSV文件中的行

您好,我正在尝试制作一个更新csv中值的程序。用户搜索ID,如果ID存在,它将在该ID号所在的行上获取要替换的新值。这row[0:9]是我的身份证的长度。

我的想法是从0-9扫描每行或我的ID编号在哪里,当找到它时,我将使用.replace()方法替换除它以外的值。这是我的方法:

    def update_thing():
        replace = stud_ID +','+ stud_name +','+ stud_course +','+ stud_year
        empty = []
        with open(fileName, 'r+') as upFile:
            for row in f:
                if row[0:9] == stud_ID:
                    row=row.replace(row,replace)
                    msg = Label(upd_win, text="Updated Successful", font="fixedsys 12 bold").place(x=3,y=120)
                if not row[0:9] == getID:
                    empty.append(row)

        upFile.close()
        upFile = open(fileName, 'w')
        upFile.writelines(empty)
        upFile.close()  
Run Code Online (Sandbox Code Playgroud)

但这不起作用,我需要有关如何解决此问题的想法。

屏幕截图

python csv

7
推荐指数
1
解决办法
2万
查看次数

NameError:未定义全局名称"flash"

我正在通过以下教程构建基本的Flask应用程序来学习数据库:https://www.tutorialspoint.com/flask/flask_sqlalchemy.htm我目前正处于需要连接到SQLITE3数据库的位置通过SQLAlchemy.说实话,它并不顺利.我收到此错误:NameError:未定义全局名称'flash'

这是我的app.py.

#!/usr/bin/python

from flask import Flask, render_template, json, request
from flask_sqlalchemy import SQLAlchemy
import sqlite3



conn = sqlite3.connect('test.db')




app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///students.sqlite3'
db = SQLAlchemy(app)


class Students(db.Model):
    id = db.Column('student_id',db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True)

    def __init__(self, name, id):
        self.name = name
        self.id = id

    def __repr__(self):
        return '<Students %r>' % self.name


db.create_all()


@app.route("/")
def main():
    return render_template('index.html')




@app.route('/new', methods = ['GET', 'POST'])
def new():
   if request.method == 'POST':
      if not request.form['name'] or not …
Run Code Online (Sandbox Code Playgroud)

python sqlite sqlalchemy flask-sqlalchemy

3
推荐指数
1
解决办法
3134
查看次数

标签 统计

python ×2

csv ×1

flask-sqlalchemy ×1

sqlalchemy ×1

sqlite ×1