我试图制作一个时间序列图,我每秒钟都有大约50秒的数据点(在我的情况下是UTC)。 Python在我的情节的x轴上对我的数据数据大吼大叫,如下所示:
%run "C:/Users/Jeff/Desktop/Python/STEPS_data.py"
File "C:\Users\Jeff\Desktop\Python\STEPS_data.py", line 3
x = [23:13:51,23:13:52,23:13:53,23:13:54,23:13:55,23:13:56,23:13:57,23:13:58,23:13:59,23:14:00,23:14:01,23:14:02,23:14:03,23:14:04,23:14:05,23:14:06,23:14:07,23:14:08,23:14:09,23:14:10,23:14:11,23:14:12,23:14:13,23:14:14,23:14:15,23:14:16,23:14:17,23:14:18,23:14:19,23:14:20,23:14:21,23:14:22,23:14:23,23:14:24,23:14:25,23:14:26,23:14:27,23:14:28,23:14:29,23:14:30,23:14:31,23:14:32,23:14:33,23:14:34,23:14:35,23:14:36]
^
SyntaxError: invalid syntax
在此之后还有一些关于该情节的其他信息,但它会挂在这一行上,它表示我在数组元素23:14:23
的第一个冒号处有一个无效的语法错误,这对我来说真的没有意义。我尝试使数组成为自己的变量x1
,只是说x = x1
,但这只会将语法错误点推回一个字符。
这似乎是一个非常愚蠢的问题,但我很难过。
答案 0 :(得分:1)
问题是在任何地方都不允许:
,例如:
>>> a = 10:2
File "<ipython-input-12-63c21fb7e990>", line 1
a = 10:2
^
SyntaxError: invalid syntax
我认为你想要它们作为字符串(在字符串中允许:
):
l = ['23:13:51', '23:13:52', '23:13:53', '23:13:54', '23:13:55', '23:13:56',
'23:13:57', '23:13:58', '23:13:59', '23:14:00', '23:14:01', '23:14:02', '23:14:03',
'23:14:04', '23:14:05', '23:14:06', '23:14:07', '23:14:08', '23:14:09', '23:14:10',
'23:14:11', '23:14:12', '23:14:13', '23:14:14', '23:14:15', '23:14:16', '23:14:17',
'23:14:18', '23:14:19', '23:14:20', '23:14:21', '23:14:22', '23:14:23', '23:14:24',
'23:14:25', '23:14:26', '23:14:27', '23:14:28', '23:14:29', '23:14:30', '23:14:31',
'23:14:32', '23:14:33', '23:14:34', '23:14:35', '23:14:36']
如果您不想手动添加所有这些''
,只需将整个内容包装为字符串并将其拆分:
>>> l = "[23:13:51,23:13:52,23:13:53,23:13:54,23:13:55,23:13:56,23:13:57,23:13:58,23:13:59,23:14:00,23:14:01,23:14:02,23:14:03,23:14:04,23:14:05,23:14:06,23:14:07,23:14:08,23:14:09,23:14:10,23:14:11,23:14:12,23:14:13,23:14:14,23:14:15,23:14:16,23:14:17,23:14:18,23:14:19,23:14:20,23:14:21,23:14:22,23:14:23,23:14:24,23:14:25,23:14:26,23:14:27,23:14:28,23:14:29,23:14:30,23:14:31,23:14:32,23:14:33,23:14:34,23:14:35,23:14:36]"
>>> l[1:-1].split(',')
或者你想要它们作为datetime
s?
>>> import datetime
>>> [datetime.datetime.strptime(t, '%H:%M:%S') for t in l[1:-1].split(',')]
或time
s?
>>> [datetime.datetime.strptime(t, '%H:%M:%S').time() for t in l[1:-1].split(',')]