python读取excel文件并处理日期格式(xlrd)

python读取excel文件并处理日期格式(xlrd)


def import_data(request):

if request.method == "GET":
return render(request, 'shopincomes/upload_file.html')

else:
file = request.FILES.get('file')
file_path = os.path.join(UPLOAD_FILE, file.name) #先读取文件保存
with open(file_path, "wb") as f:
for line in file.chunks():
f.write(line)

  import xlrd
  from datatime import date
work_book = xlrd.open_workbook(file_path) #然后在打开文件读取写入数据库
sheet_name = work_book.sheet_by_name('店铺信息')
if sheet_name.nrows <= 1:
return HttpResponse('上传的文件为空或者其他错误')

rows = sheet_name.nrows
data = {}
for i in range(1, rows):
rowvalues = sheet_name.row_values(i)
data['code'] = rowvalues[0]
data['name'] = rowvalues[1]
data['shop_type'] = rowvalues[2]
data['city'] = rowvalues[3]
data['area'] = rowvalues[4]
data['state'] = rowvalues[5]
issudate = sheet_name.cell(i, 6).value
data_value = xlrd.xldate_as_tuple(issudate, work_book.datemode)
tmp = date(*data_value[:3]).strftime('%Y-%m-%d')
print(type(tmp), tmp)
data['open_date'] = tmp
ShopInfo.objects.create(**data)

return redirect(reverse('shopincomes:shop_info_view'))
 

 

posted on 2019-04-10 20:47  nickshen  阅读(12155)  评论(0编辑  收藏  举报

导航