在python中可以直接通过open打开指定文件,通过for循环可以直接读取文件的每一行。另外django中我们可以通过model创建数据模型,但如果是已经存在的表,直接写入txt的数据,可以通过建立连接执行添加数据。
def insertData():
try:
conn = MySQLdb.connect(host="localhost", user="root", passwd="***", db="test", charset="utf8")
with conn:
cursor = conn.cursor()
f = open('.../201501.txt')
next(f) # 从文件的第二行开始读起
for line in f:
linelist = line.split( ) #