导入包括字段之一为空的Excel时如何解决引发ValueError(“未知字符串格式:”,timestr)为空

时间:2019-06-17 11:26:05

标签: python-dateutil

django openpyxl为null时出现

datefield错误

我想导入Excel工作表,但某些日期字段为空。

class Drgmt(models.Model):
      date_ori = models.DateTimeField(default=None, null=True, blank=True)


def import(request):
    #Many lines before
    date_ori = dt.datetime.strftime(parse(row_data[19]), '%Y-%m-%d %H:%M:%S')
    #Many lines after

回溯(最近通话最近一次):

  File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\core\handlers\exception.py", l
ine 34, in inner
    response = get_response(request)
  File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\core\handlers\base.py", line 1
15, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\AppData\Roaming\Python\Python37\site-packages\django\core\handlers\base.py", line 1
13, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Desktop\performance\cuivre\views.py", line 57, in importeradsl date_ori = dt.datetime.strftime(parse(row_data[19]), '%Y-%m-%d %H:%M:%S')
File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\dateutil\parser\_parser.py", li
ne 1356, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\dateutil\parser\_parser.py", li
ne 648, in parse
    raise ValueError("Unknown string format:", timestr)
ValueError: ('Unknown string format:', 'None')
[18/Jun/2019 08:37:02] "POST /cuivre/ HTTP/1.1" 500 85133

1 个答案:

答案 0 :(得分:0)

好吧,错误消息很清楚:您将字符串'None'作为timestr的{​​{1}}参数传递,并且解析器显然无法将其解析为日期

解决方案也很明显:handle the exception

dateutil.parser.parse()