python-ValueError:时间数据与格式不匹配 [英] python - ValueError: time data does not match format

查看:518
本文介绍了python-ValueError:时间数据与格式不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从

I downloaded my CSV file TSLA.csv from here. It has a header line and 7 columns, first of which is date, the others are floats and ints.

我希望能够得到一个numpy数组:

I want to be able to get a numpy array out of it:

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


def bytespdate2num(fmt, encoding="utf-8"):
    strconverter = mdates.strpdate2num(fmt)
    def bytesconverter(b): 
        s = b.decode(encoding)
        return strconverter(s)
    return bytesconverter

with open("TSLA.CSV", "r") as csvfile:
    stock_price = csv.reader(csvfile, delimiter=" ")
    stock_price = list(stock_price)


date, closep, highp, lowp, openp, volume = np.loadtxt(stock_price, 
                                                      delimiter = ",",
                                                      unpack = True,
                                                      skiprows=1,
                                                      converters={0: bytespdate2num("%Y-%m-%d")})

问题是,出现以下错误:

Trouble is, I get the following error:

ValueError:时间数据"['2010-06-29"与格式'%Y-%m-%d'不匹配

ValueError: time data "['2010-06-29" does not match format '%Y-%m-%d'

我已经仔细检查了格式,并在此处检查了其他问题,但是这些问题主要是关于错误的格式...我在这里看不到问题.帮助表示赞赏.

I have double-checked the format and checked other questions around here, but those were mostly about the wrong format... I can't see the problem here. Help appreciated.

推荐答案

好的,我相信问题在这里:

Okay, I believe the problem is here:

再次查看错误,它显示为"['2010-06-29" does not match data format. 那是因为您不是试图单独解析日期,而是查看日期周围的双引号.

Have a look again at the error, it says "['2010-06-29" does not match data format. That is because you are not trying to parse the date alone, look at the double quotes that surround the date.

您正在尝试解析:

['2010-06-29

您的字符串中还有一个额外的['会使事情混乱.

There's an extra [' in your string that is messing things up.

这篇关于python-ValueError:时间数据与格式不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆