使用 python 更新 csv 文件
update with csv file using python
我必须用 CSV 文件更新数据库。考虑数据库 table 如下所示:
CSV 文件数据如下所示:
如您所见,CSV 文件数据修改了一些数据并添加了一些新记录,我应该做的是仅更新修改过的数据或添加的一些新记录。
在表2中,修改了col2的第一条记录。我只需要更新col2的第一条记录(即AA),而不需要更新col2的全部记录。
我可以通过硬编码来做到这一点,但我不想通过硬编码来做到这一点,因为我需要用 2000 tables 来做到这一点。
任何人都可以建议我实现目标的步骤。
这是我的代码片段..
df = pd.read_csv('F:\filename.csv', sep=",", header=0, dtype=str)
sql_query2 = engine.execute('''
SELECT
*
FROM ttcmcs023111temp
''')
df2 = pd.DataFrame(sql_query2)
df.update(df2)
因为我没有和你类似的数据,所以我用的是自己的数据库。
我的书table的架构如下:
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | NO | | NULL | |
| author | char(30) | NO | | NULL | |
+--------+-------------+------+-----+---------+-------+
table 看起来像这样:
+----+--------------------+------------------+
| id | name | author |
+----+--------------------+------------------+
| 1 | Origin | Dan Brown |
| 2 | River God | Wilbur Smith |
| 3 | Chromosome 6 | Robin Cook |
| 4 | Where Eagles Dare | Alistair Maclean |
| 5 | The Seventh Scroll | Dan Brown | ### Added wrong entry to prove
+----+--------------------+------------------+ ### my point
因此,我的方法是使用 python 从 CSV 中创建一个新的临时 table,其架构与书籍 table 相同。
我使用的代码如下:
sql_query = sqlalchemy.text("CREATE TABLE temp (id int primary key, name varchar(30) not null, author varchar(30) not null)")
result = db_connection.execute(sql_query)
csv_df.to_sql('temp', con = db_connection, index = False, if_exists = 'append')
这会创建一个 table 像这样:
+----+--------------------+------------------+
| id | name | author |
+----+--------------------+------------------+
| 1 | Origin | Dan Brown |
| 2 | River God | Wilbur Smith |
| 3 | Chromosome 6 | Robin Cook |
| 4 | Where Eagles Dare | Alistair Maclean |
| 5 | The Seventh Scroll | Wilbur Smith |
+----+--------------------+------------------+
现在,您只需使用 MySQL 中的 update
使用 INNER JOIN
来更新您想要在原始 table 中更新的值。 (在我的例子中,'books')。
操作方法如下:
statement = '''update books b
inner join temp t
on t.id = b.id
set b.name = t.name,
b.author = t.author;
'''
db_connection.execute(statement)
此查询将从我使用 CSV 创建的 table temp
更新 table books
中的值。
您可以在更新值后销毁 temp
table。
我必须用 CSV 文件更新数据库。考虑数据库 table 如下所示:
CSV 文件数据如下所示:
如您所见,CSV 文件数据修改了一些数据并添加了一些新记录,我应该做的是仅更新修改过的数据或添加的一些新记录。
在表2中,修改了col2的第一条记录。我只需要更新col2的第一条记录(即AA),而不需要更新col2的全部记录。
我可以通过硬编码来做到这一点,但我不想通过硬编码来做到这一点,因为我需要用 2000 tables 来做到这一点。
任何人都可以建议我实现目标的步骤。
这是我的代码片段..
df = pd.read_csv('F:\filename.csv', sep=",", header=0, dtype=str)
sql_query2 = engine.execute('''
SELECT
*
FROM ttcmcs023111temp
''')
df2 = pd.DataFrame(sql_query2)
df.update(df2)
因为我没有和你类似的数据,所以我用的是自己的数据库。 我的书table的架构如下:
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | NO | | NULL | |
| author | char(30) | NO | | NULL | |
+--------+-------------+------+-----+---------+-------+
table 看起来像这样:
+----+--------------------+------------------+
| id | name | author |
+----+--------------------+------------------+
| 1 | Origin | Dan Brown |
| 2 | River God | Wilbur Smith |
| 3 | Chromosome 6 | Robin Cook |
| 4 | Where Eagles Dare | Alistair Maclean |
| 5 | The Seventh Scroll | Dan Brown | ### Added wrong entry to prove
+----+--------------------+------------------+ ### my point
因此,我的方法是使用 python 从 CSV 中创建一个新的临时 table,其架构与书籍 table 相同。 我使用的代码如下:
sql_query = sqlalchemy.text("CREATE TABLE temp (id int primary key, name varchar(30) not null, author varchar(30) not null)")
result = db_connection.execute(sql_query)
csv_df.to_sql('temp', con = db_connection, index = False, if_exists = 'append')
这会创建一个 table 像这样:
+----+--------------------+------------------+
| id | name | author |
+----+--------------------+------------------+
| 1 | Origin | Dan Brown |
| 2 | River God | Wilbur Smith |
| 3 | Chromosome 6 | Robin Cook |
| 4 | Where Eagles Dare | Alistair Maclean |
| 5 | The Seventh Scroll | Wilbur Smith |
+----+--------------------+------------------+
现在,您只需使用 MySQL 中的 update
使用 INNER JOIN
来更新您想要在原始 table 中更新的值。 (在我的例子中,'books')。
操作方法如下:
statement = '''update books b
inner join temp t
on t.id = b.id
set b.name = t.name,
b.author = t.author;
'''
db_connection.execute(statement)
此查询将从我使用 CSV 创建的 table temp
更新 table books
中的值。
您可以在更新值后销毁 temp
table。