我有一个复杂的表(几百列),我不时地在csv文件中获取数据。我所要做的就是用csv文件中的数据更新表。
这就是我现在使用的:没什么特别的(我猜不是使用Python的力量?)。如果有人能将其翻译成Pythonic,那就太棒了!
我们的想法是匹配MyTable和Field_Mapping中的列名,并相应地进行更新。如果列具有乘法值,则在插入/更新时将乘以该值。
对于凌乱的Python代码感到抱歉,我尽力让它变得可读。
MyDB.MyTable
------------
PK DateAdded Firm Addr1 Website
------------------------------------
1 2011-01-01 ABC 1 Main St abc.com
MyDB.Field_Mapping
------------------
SourceColumns TargetTableColumns Multiply
-----------------------------------------
PK PK
webaddr Website
address Addr1
assets value x1000
Python代码:
import string, os, sys
# DB Conn String here
cursor = db.cursor()
cursor2 = db.cursor()
cursor3 = db.cursor()
TableName = sys.argv[1]
cursor.execute("select * from `" + TableName + "` limit 1")
for cursorFieldname in cursor.description:
cursor2.execute("select TargetTableColumns from MyDB.Field_Mapping where FDIC = \"" + cursorFieldname[0] + "\"")
row = cursor2.fetchone()
if row > -1:
cursor2.execute("alter table `" + TableName + "` change `" + cursorFieldname[0] + "` `" + str(row[0]) + "` varchar(255)")
cursor3.execute("create index PKIndx on `" + filename + "`(PK);")
cursor3.execute("insert ignore into MyDB.MyTable (PK, dateadded) select PK, now() from `" + TableName + "`;")
cursor3.execute("select count(*) from `" + filename + ";")
row2 = cursor3.fetchone()
if str(row2[0]) > "1000": #Deleting PKs > 1000
cursor3.execute("delete from MyDB.MyTable where PK < \"a\" and PK not in (select PK from `" + filename + "`);")
cursor.execute("select * from `" + filename + "` limit 1")
for cursorFieldname in cursor.description:
cursor2.execute("select * from MyDB.MyTable limit 1")
for cursorFieldname2 in cursor2.description:
if cursorFieldname[0].lower() == cursorFieldname2[0].lower():
cursor3.execute("select multiply from MyDB.Field_Mapping where TargetTableColumns = \"" + cursorFieldname[0] + "\"")
row2 = cursor3.fetchone()
if str(row2[0]) == "x1000":
cursor3.execute("update MyDB.MyTable as a, `" + filename + "` as b set a.`" + cursorFieldname[0] + "` = b.`" + cursorFieldname[0] + "`*1000 where a.PK = b.PK;")
elif str(row2[0]) == "%":
cursor3.execute("update MyDB.MyTable as a, `" + filename + "` as b set a.`" + cursorFieldname[0] + "` = round(b.`" + cursorFieldname[0] + "`, 2) where a.PK = b.PK;")
else:
if cursorFieldname[0] == "addr1":
cursor3.execute("update MyDB.MyTable as a, `" + filename + "` as b set a.`" + cursorFieldname[0] + "` = b.`" + cursorFieldname[0] + "` where a.PK = b.PK and b.`" + cursorFieldname[0] + "` != \"Main Street\";")
elif cursorFieldname[0] != "PK":
if cursorFieldname[0].lower() == "website":
cursor3.execute("update `" + filename + "` set website = lcase(website)")
cursor3.execute("update MyDB.MyTable as a, `" + filename + "` as b set a.`" + cursorFieldname[0] + "` = b.`" + cursorFieldname[0] + "` where a.PK = b.PK;")
cursor.close()
cursor2.close()
cursor3.close()
db.close()
答案 0 :(得分:1)
我首先将上面的代码表示为简单的英语算法。将问题分解为你想要完成的工作的基本部分而不是你想要做什么通常会大大简化事情。
如果您想简化访问,请查看database abstraction layers。 SQLAlchemy与我所听到的相比非常好。
就个人而言,如果你的代码有效(这不是很多代码),为什么要改变呢? : - )