stack overflow网站不错https://stackoverflow.com
问题一:strptime() argument 0 must be str, not <class 'bytes'>
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mydates
date, open, close=np.loadtxt(r'd:\000001.csv',delimiter=',', converters={0:mydates.strpdate2num('%m/%d/%Y')},
skiprows=1, usecols=(0, 1, 4), unpack=True)
错误提示:Exception "unhandled TypeError"
strptime() argument 0 must be str, not <class 'bytes'>
通过转换函数convertors,把字符串转(date)换成matplotlib能够识别的数据类型(浮点),把x轴转换成时间,matplotlib另一个函数plot_date(date,open),把date数据转换成时间格式。
关于TypeError: strptime() argument 1 must be str, not bytes解析 - 樟樟22 - 博客园 https://www.cnblogs.com/zz22--/p/7496345.html
解决办法:在python2.x中可直接使用:strpdate2num();而在python3.x中则要使用:bytespdate2num()
The workaround of unutbu work perfectly fine. Meanwhile it seems that the issue has been addressed. Using bytespdate2num() instead of strpdate2num() works for me.
问题二:embedded null character
date,open,close=np.loadtxt('d:\cems\000001.csv',delimiter=',',
converters={0:mydates.bytespdate2num('%m/%d/%Y')},skiprows=1,usecols=(0,1,4),unpack=True)
提示错误:Exception "unhandled ValueError"embedded null character
File: D:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\npyio.py, Line: 902,npyio.py中提示具体如下:
try:
if is_pathlib_path(fname):
fname = str(fname)
if _is_string_like(fname):
fown = True
if fname.endswith('.gz'):
import gzip
fh = iter(gzip.GzipFile(fname))
elif fname.endswith('.bz2'):
import bz2
fh = iter(bz2.BZ2File(fname))
elif sys.version_info[0] == 2:
fh = iter(open(fname, 'U'))
else:
fh = iter(open(fname))
else:
fh = iter(fname)
except TypeError:
raise ValueError('fname must be a string, file handle, or generator')
根据提示,检查文件中的行号对应的语句,应该是路径出现错误。
'd:\cems\000001.csv',改成r'd:\cems\000001.csv',问题解决。
参考:https://baike.baidu.com/item/%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6/86397?fr=aladdin
路径中的“\0”转义字符表示空字符(NULL)。
参考重要:python3和Python2的区别(被坑太久了)https://www.cnblogs.com/hanggegege/p/5840005.html
特别注意:python3中与python2有很多函数不一致,程序编写中造成许多错误。
Python2 和 python3 的 unicode 与 str 总结 https://blog.csdn.net/jobschen/article/details/79805955