简体   繁体   English

strptime()参数0必须是str,而不是<class 'bytes'>

[英]strptime() argument 0 must be str, not <class 'bytes'>

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

date,open,close=np.loadtxt('000001.csv',delimiter=',',
                            converters={0:mdates.strpdate2num('%m/%d/%Y')},
                            skiprows=1, usecols=(0,1,4), unpack=True)

plt,plot(date,open)

But strptime() argument 0 must be str, not 但是strptime()参数0必须是str,而不是

I have read all of about answers in the website, but these don't help me. 我已经在网站上阅读了所有关于答案的信息,但是这些对我没有帮助。

You need to decode the bytes loadtxt reads from the file. 您需要解码loadtxt从文件读取的字节。

Write a helper function: 编写一个辅助函数:

def convert_date(date_bytes):
    return mdates.strpdate2num('%m/%d/%Y')(date_bytes.decode('ascii'))

and use it as converter: 并将其用作转换器:

date, open, close = np.loadtxt('000001.csv',delimiter=',',
                               converters={0: convert_date},
                               skiprows=1, usecols=(0,1,4), unpack=True)

If .decode('ascii') doesn't work, try a different encoding. 如果.decode('ascii')不起作用,请尝试其他编码。 Best would be to find out what the encoding of the file is. 最好是找出文件的编码是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM