from base_api import utils
from openpyxl import load_workbook
from iptv.models import TestUser
wb = load_workbook(filename = 'F:\\aa.xlsx')
sheet = wb .worksheets[0]
data = []
head = []
for index,row in enumerate(sheet.rows):
t = []
for c in row:
t.append(c.value)
if index == 0:
head = t
else:
data.append(dict(zip(head,t)))
print 'data===',data
result = []
#{u'gc': u'103389391', u'zy': 1371986972.0, u'yyx': 107495135.0}
for elem in data:
if "gc" in elem:
result.append(TestUser(userid = elem["gc"],ctype="gc"))
if "zy" in elem:
result.append(TestUser(userid = elem["zy"],ctype="zy"))
if "yyx" in elem:
result.append(TestUser(userid = elem["yyx"],ctype="yyx"))
print len(result),result
while result:
p = result[:3000]
with transaction.atomic():
for e in p:
e.save()
result = result[3000:]
from openpyxl import load_workbook
from iptv.models import TestUser
wb = load_workbook(filename = 'F:\\aa.xlsx')
sheet = wb .worksheets[0]
data = []
head = []
for index,row in enumerate(sheet.rows):
t = []
for c in row:
t.append(c.value)
if index == 0:
head = t
else:
data.append(dict(zip(head,t)))
print 'data===',data
result = []
#{u'gc': u'103389391', u'zy': 1371986972.0, u'yyx': 107495135.0}
for elem in data:
if "gc" in elem:
result.append(TestUser(userid = elem["gc"],ctype="gc"))
if "zy" in elem:
result.append(TestUser(userid = elem["zy"],ctype="zy"))
if "yyx" in elem:
result.append(TestUser(userid = elem["yyx"],ctype="yyx"))
print len(result),result
while result:
p = result[:3000]
with transaction.atomic():
for e in p:
e.save()
result = result[3000:]
本文介绍如何使用Python从Excel文件中读取数据,并批量创建数据库中的用户记录。通过遍历Excel表格并处理每行数据,根据指定字段创建不同类型的用户对象。
6507

被折叠的 条评论
为什么被折叠?



