import os
#导入数据库用
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hmd_1.settings")
'''
Django 版本大于等于1.7的时候,需要加上下面两句
import django
django.setup()
否则会抛出错误 django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
'''
import django
if django.VERSION >= (1, 7): # 自动判断版本
django.setup()
from add1.models import userinfo
def main():
f = open('2.csv')
for line in f:
row = line.split(',')
all = {}
all['f1'] = row[0]
all['获取日期'] = row[1]
all['姓名'] = row[2]
all['身份证号'] = row[3]
all['手机号'] = row[4]
all['本金/本息'] = row[5]
all['执行日期'] = row[6]
all['发生案例'] = row[7]
all['未还/罚息'] = row[8]
all['已还金额'] = row[9]
all['逾期次数'] = row[10]
all['邮箱地址'] = row[11]
all['借款期数'] = row[12]
all['逾期天数'] = row[13]
if all['逾期天数'] == '\n':
all['逾期天数'] = ''
userinfo.objects.create( xh = all['f1'],
get_date = all['获取日期'],
name = all['姓名'],
ID_number = all['姓名'],
phone = all['手机号'],
Principal_principal= all['本金/本息'],
Execution_date = all['执行日期'],
Case_of_occurrence = all['发生案例'],
Unpaid_fine = all['未还/罚息'],
Amount_returned = all['已还金额'],
Overdue_number = all['逾期次数'],
E_mail = all['邮箱地址'],
Loan_period = all['借款期数'],
Overdue_days = all['逾期天数'])
f.close()
if __name__ == "__main__":
main()
print('Done!')
数据重复导入怎么办
# 只要把上面的
#
# 1
# Blog.objects.create(title=title,content=content)
# 换成下面的就不会重复导入数据了
#
# 1
# Blog.objects.get_or_create(title=title,content=content)