Python 属性错误:';str';对象没有属性';第'天;尝试将日期和时间放入数组时出错
我试图打开csv文件,然后将日期拉入日期数组,将时间拉入时间数组,但我遇到了以下错误“AttributeError:'str'对象没有属性'day'”。我在其中一个文件中添加了一个图像Python 属性错误:';str';对象没有属性';第'天;尝试将日期和时间放入数组时出错,python,file,python-3.x,Python,File,Python 3.x,我试图打开csv文件,然后将日期拉入日期数组,将时间拉入时间数组,但我遇到了以下错误“AttributeError:'str'对象没有属性'day'”。我在其中一个文件中添加了一个图像 data = {'days': [], 'times': []} with open(open_file) as in_f: reader = DictReader(in_f) for line in reader: data['day'].append(line
data = {'days': [],
'times': []}
with open(open_file) as in_f:
reader = DictReader(in_f)
for line in reader:
data['day'].append(line['time_received_isoformat'].day)
data['time'].append(line['time_received_isoformat'].hour * 60 + line['time_received_isoformat'].minute)
data = DataFrame(data)
plot = seaborn.stripplot(data=data, x='day', y='time')
plot.get_figure().savefig('/home/jacob/Projects/CIS2302/CW2/ddd_cw2/temporal_graphs/' + 'days_stripplot' + '.png')
pyplot.close()
in\u f
是一个文件对象,reader
是该文件的DictReader
对象,line
是一个字符串字典<因此,代码>行['time\u received\u isoformat']解析为字符串,因为CSV文件包含字符串。字符串没有day
属性。如果希望将它们解析为具有day
属性的date
或datetime
对象,则需要将该字符串解析为此类对象(具体格式取决于您拥有的确切格式),然后再尝试查找其day
行['time\u received\u isoformat']似乎返回一个字符串,而不是一个单词。试着打印(行['time\u received\u isoformat'])看看它给你带来了什么,但我不知道怎么解决它…@JacobFlynntime\u received\u isoformat
看起来像什么?时间戳有几种有效的ISO格式。我看不到您的CSV文件的内容,所以我不能确切地告诉您该如何解析该字段。你需要告诉你的程序需要什么样的字符串以及如何处理它。time\u received\u isoformat 2015-06-01T00:00:00 2015-06-01T00:00:12
我刚刚从文件中复制了这个@TigerhawkT3@JacobFlynn:您可以在python标准库time
模块中阅读有关时间解析的内容: