【问题标题】:Time data 'None' does not match format '%H:%M:%S'时间数据“无”与格式“%H:%M:%S”不匹配
【发布时间】:2019-01-01 21:12:33
【问题描述】:

这里我有时间值,我使用 python 绘制从 Excel.csv 文件导入的图形。在这里,我有一些没有的行。当我对其进行编码时,显示时间数据 none 与 '%H:%M:%S' 不匹配的错误。 在这里,我将时间与值表包括在内。

x 数据 y 数据 x1 数据 y1 数据 x2 数据 y2 数据
0:06:15 141 0:08:00 131 0:06:45 136
0:09:25 95 0:08:15 117 0:09:30 95
0:11:00 149 0:08:30 109 0:11:30 139
0:13:50 85 0:08:45 103 0:13:30 95
0:16:25 135 0:09:00 97 0:15:25 105
0:19:00 63 无 无 0:18:00 97
0:20:00 111 无 无 0:19:30 100
0:22:05 115 无 无 0:22:15 115
0:23:40 287 无 无 无 无

我的代码是,

condition = ""
weight = ""
height = ""
date = ""

label1 = ""
label2 = ""
label3 = ""

x  = []
y  = []
x1 = []
y1 = []
x2 = []
y2 = []


def convertTime(s):
	tm = time.strptime(s, "%H:%M:%S")
	return datetime.datetime(date.tm_year,date.tm_mon, date.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec)

with open('data.csv') as csv_file:
  csv_data = csv.reader(csv_file, delimiter=',')
  row_num = 0
  for row in csv_data:
    if(row_num == 0):
      condition = row[0]
      weight = row[1]
      height = row[2]
    elif(row_num == 1):
      date = time.strptime(row[0], "Date:-%Y/%m/%d")
    elif(row_num == 2):
      label1 = row[0]
      label2 = row[1]
      label3 = row[2]
    elif(row_num > 3): #Data starts here
      x.append(convertTime(row[0]))
      y.append(int(row[1]))
      x1.append(convertTime(row[2]))
      y1.append(int(row[3]))
      x2.append(convertTime(row[4]))
      y2.append(int(row[5]))
    row_num = row_num + 1

plt.plot(x,y,label=label1)
plt.stem(x1,y1,'C1-.','C1o',label=label2)
plt.stem(x2,y2,'C2-.','C2o',label=label3)
plt.legend()
plt.gcf().autofmt_xdate()
plt.show()

print(x)
print(y)

我的错误是,

ValueError:时间数据“无”与格式“%H:%M:%”不匹配。 谁能帮帮我?

【问题讨论】:

  • 您的问题是什么?你很明显有None 值。你知道你有None 值。您尝试将这些 None 值解析为日期时间字符串,并收到一条错误消息,告诉您不适用于 None 值,因为它们不是日期时间字符串。
  • 您不应将条件放在括号中。这实际上创建了一个元组。只需用空格将条件与if 分开...关于您的问题,请阅读Exception Handling...当您回复评论时,如果您想解决某个用户,请以“@UserName”开头。
  • @Sven Krüger,当然。

标签: python jupyter-notebook


【解决方案1】:

您要做的是将 None 解析为 datetime 对象,但在此之前,您需要告诉 datetime 您的日期格式

在您的代码中 -

def convertTime(s):
    # when s = None, you get that error 
    tm = time.strptime(s, "%H:%M:%S")
    return datetime.datetime(date.tm_year,date.tm_mon, date.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec)

要处理此错误,您可以检查 s 是否为 not None 之类的 -

def convertTime(s):
    if s:
        tm = time.strptime(s, "%H:%M:%S")
        return datetime.datetime(date.tm_year,date.tm_mon, date.tm_mday,
    return None

但即便如此,您的图表也会出错,因为您在x1x2 列中缺少数据。但我认为这就是你能做的或正确获取数据的全部。

或者您可以检查这些列中是否包含 None 并将 continue 放入您的 for 循环中并完全跳过该行,例如 -

elif(row_num > 3): #Data starts here
    if None in row:
        row_num += 1
        continue # This will make you skip the row
    x.append(convertTime(row[0]))
    y.append(int(row[1]))
    x1.append(convertTime(row[2]))
    y1.append(int(row[3]))
    x2.append(convertTime(row[4]))
    y2.append(int(row[5]))

您将跳过刻度,但您将能够绘制图表

【讨论】:

  • 我无法绘制它。它显示错误,如文件“”,第 37 行,如果 None 在行中:^ SyntaxError: invalid syntax
  • @kasuri 删除 None 周围的引号,看看你的缩进是否正确并告诉我
  • 我删除了所有无并运行我的代码然后它显示错误,例如 ValueError: time data '' does not match format '%H:%M:%S'
  • 我遇到了一个错误,row_num += 1 (num) -IndentationError: expected an indented block
  • 确保你的空格是正确的,这就是错误所说的
猜你喜欢
  • 2022-12-06
  • 1970-01-01
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
  • 2019-05-09
  • 1970-01-01
  • 2017-06-04
  • 2017-03-05
相关资源
最近更新 更多