如何为np.savetxt正确添加head,从list和np..ndarray转换为我需要的?

2024-09-27 04:21:43 发布

您现在位置:Python中文网/ 问答频道 /正文


with open('127case.csv','r') as f:
    file = csv.reader(f)
    rows = [row for row in file]

head1 = rows[0]

#head1 = ['0:00', '0:30', '1:00', '1:30', '2:00', '2:30', '3:00', '3:30', '4:00', '4:30', '5:00', '5:30', '6:00', '6:30', '7:00', '7:30', '8:00', '8:30', '9:00', '9:30', '10:00', '10:30', '11:00', '11:30', '12:00', '12:30', '13:00', '13:30', '14:00', '14:30', '15:00', '15:30', '16:00', '16:30', '17:00', '17:30', '18:00', '18:30', '19:00', '19:30', '20:00', '20:30', '21:00', '21:30', '22:00', '22:30', '23:00', '23:30']

print(type(head1))        #it's list

head2 = np.array(['0:00' '0:30' '1:00' '1:30' '2:00' '2:30' '3:00' '3:30' '4:00' '4:30'
 '5:00' '5:30' '6:00' '6:30' '7:00' '7:30' '8:00' '8:30' '9:00' '9:30'
 '10:00' '10:30' '11:00' '11:30' '12:00' '12:30' '13:00' '13:30' '14:00'
 '14:30' '15:00' '15:30' '16:00' '16:30' '17:00' '17:30' '18:00' '18:30'
 '19:00' '19:30' '20:00' '20:30' '21:00' '21:30' '22:00' '22:30' '23:00'
 '23:30'])

print(type(head2))     #it's <class 'numpy.ndarray'>

data2 = np.zeros([1*1,48])
np.savetxt('test.csv',data2,fmt='%.3f',header = head1,delimiter=',', comments='')

当我尝试在head1中使用表单保存它时,它显示:

AttributeError: 'list' object has no attribute 'replace'

当我尝试在head2中保存表单时,它显示:

AttributeError: 'numpy.ndarray' object has no attribute 'replace'

适当的形式应为:

head3 = " 0:00, 0:30, 1:00, 1:30,.....,"

如何从头1和头2实现此表单

提前谢谢


Tags: csvnumpy表单typenpitlistfile

热门问题