2016-05-13 90 views
-1

我试图打开csv文件,然后将天数拉入days数组和时间数组中,但是我得到这个错误“AttributeError:'str'对象没有属性'day'”。我加入的图像什么的一个文件看起来像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['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() 
+0

line ['time_received_isoformat']似乎返回一个字符串不是一个dictionnay。尝试打印(行['time_received_isoformat']),看看它给你什么 –

+0

Ino,但我不知道该怎么做才能解决它... – Jake123

回答

0

in_f是一个文件对象,readerDictReader对象与该文件,并line是一个字符串的字典。 line['time_received_isoformat'],因此,解析为一个字符串,因为CSV文件包含字符串。字符串没有day属性。如果您希望将它们解析为datedatetime对象,该对象具有day属性,则在尝试查找其day之前,需要将该字符串解析为此对象(根据具体格式的不同,该对象会有所不同)。

+0

你是什么意思什么格式即时通讯使用? – Jake123

+0

@JacobFlynn'time_received_isoformat'看起来像什么?有几种有效的时间戳ISO格式。 –

+0

我看不到你的CSV文件的内容,所以我不能告诉你该字段应该如何解析。你需要告诉你的程序需要什么样的字符串以及如何处理它。 – TigerhawkT3

相关问题