简体   繁体   中英

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

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.

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. Best would be to find out what the encoding of the file is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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