TypeError Traceback (most recent call last)
in
----> 1 dates,close = np.loadtxt(‘data.csv’,delimiter=’,’,usecols=(1,6),converters={1:datestr2num},unpack=True)
2 print("Dates = ",dates)
c:\users\skysys\appdata\local\programs\python\python37-32\lib\site-packages\numpy\lib\npyio.py in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin, encoding, max_rows)
1132 # converting the data
1133 X = None
-> 1134 for x in read_data(_loadtxt_chunksize):
1135 if X is None:
1136 X = np.array(x, dtype)
c:\users\skysys\appdata\local\programs\python\python37-32\lib\site-packages\numpy\lib\npyio.py in read_data(chunk_size)
1059
1060 # Convert each value according to its column and store
-> 1061 items = [conv(val) for (conv, val) in zip(converters, vals)]
1062
1063 # Then pack it according to the dtype’s nesting
c:\users\skysys\appdata\local\programs\python\python37-32\lib\site-packages\numpy\lib\npyio.py in (.0)
1059
1060 # Convert each value according to its column and store
-> 1061 items = [conv(val) for (conv, val) in zip(converters, vals)]
1062
1063 # Then pack it according to the dtype’s nesting
c:\users\skysys\appdata\local\programs\python\python37-32\lib\site-packages\numpy\lib\npyio.py in tobytes_first(x, conv)
1118 if type(x) is bytes:
1119 return conv(x)
-> 1120 return conv(x.encode(“latin1”))
1121 import functools
1122 converters[i] = functools.partial(tobytes_first, conv=conv)
in datestr2num(s)
1 def datestr2num(s):
----> 2 return datetime.datetime.strptime(s,"%d-%m-%Y").date().weekday()
TypeError: strptime() argument 1 must be str, not bytes
编译器在打开data.csv文件时,将表格里的第2列数组值提取出来返回给dates,第二列值是日期格式字符串,但因为我们是以二进制编码的格式打开第二列值是,返回的值字节字符串bytes,所以需要把它便会string,则对字符串解码用函数decode(‘asii’),变成string格式。
def datestr2num(s):
return datetime.strptime(s.decode(‘ascii’), “%d-%m-%Y”).date().weekday()
dates=np.loadtxt(‘data.csv’, delimiter=’,’, usecols=(1,), converters={1: datestr2num}, unpack=True)
1.https://www.cnblogs.com/zz22--/p/7496345.html