zoukankan      html  css  js  c++  java
  • django数据导入

    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['逾期天数'] == '
    ':
                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)
  • 相关阅读:
    谈一谈对象池SafeObjectPool能干什么
    .net core 程序退出事件
    .NETCore 快速开发做一个简易商城
    Git创建子分支,合并分支并提交
    Vue项目中关闭eslint的方法
    Missing space before function parentheses
    单引号变双引号 格式化去掉分号
    Docker Swarm搭建多服务器下Docker集群
    Asp.Net Core中服务的生命周期选项区别和用法
    KnockoutJS-模板绑定
  • 原文地址:https://www.cnblogs.com/zj0724/p/9163989.html
Copyright © 2011-2022 走看看