[Django] l using xlrd to import data into xls files, djangoxlrd

Source: Internet
Author: User

[Django] l using xlrd to import data into xls files, djangoxlrd

Code:

# Coding: UTF-8 import OS. environ. setdefault ("DJANGO_SETTINGS_MODULE", "www. settings ") ''' when the Django version is greater than or equal to 1.7, add the following two statements: import djangodjango. setup () otherwise, the error django will be thrown. core. exceptions. appRegistryNotReady: Models aren't loaded yet. '''import djangoif django. VERSION> = (1, 7): # automatically determines the VERSION of django. setup () from keywork. models import DevDataimport xlrd # excel reading tool data = xlrd.open_workbook('cs.xls ') # open the file table = data. shee T_by_index (0) # obtain the worksheet nrows = table. nrows # number of rows ncols = table. ncols # columns colnames = table. row_values (0) WorkList = [] x = y = z = 0for I in range (1, nrows): row = table. row_values (I) # Get the value of each row for j in range (0, ncols): if type (row [j]) = float: # convert the value to int if it is float, avoid the case where output 1 is 1.0 row [j] = int (row [j]) if row: # Check whether the row value is null if DevData. objects. filter (serv_id = row [0], user_flag = row [15]). exists (): # determine whether the row value is repeated in the database x = x + 1 # Repeated value count else: y = y + 1 # non-repeated count WorkList. append (DevData (serv_id = row [0], serv_state_name = row [1], acc_nbr = row [2], user_name = row [3], acct_code = row [4], product_id = row [5], mkt_chnl_name = row [6], mkt_chnl_id = row [7], mkt_region_name = row [8], mkt_region_id = row [9], mkt_grid_name = row [10], sale_man = row [11], sale_outlets_cd1_name = row [12], completed_time = row [13], remove_data = row [14], user_flag = row [15], pro_flag = row [16 ], Service_offer_id = row [17], service_offer_name = row [18], finish_time = row [19], staff_name = row [20], staff_code = row [21], org_name = row [22], prod_offer_name = row [23], day_id = row [24],) else: z = z + 1 # empty row value count DevData. objects. bulk_create (WorkList) print 'data imported successfully, '+ str (x) +', repeat '+ str (y) +', with '+ str (z) + 'Action blank!'

 

 

One problem that arises in the middle is that when xlrd reads data, it will automatically convert all data that may be numbers in the xls cell to the python float. At this time, we will get 12.0 through str (cell. value) (assuming cell. value = 12.0 ).

Solution: for example, if my a = 1, xlrd outputs 1.0 by default. You only need int (!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.