【发布时间】:2015-03-04 23:51:07
【问题描述】:
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
import numpy as np
import time
dt = 'i4,i4,i4,a5,f9'
month,day,year,time,price = np.loadtxt('spyTestTest.txt',
delimiter=' ',dtype = dt)
我正在尝试在一些示例数据上运行此代码(如下)
8 18 2014 9:30 196.79
8 18 2014 9:31 196.8249
8 18 2014 9:32 196.825
8 18 2014 9:33 196.88
8 18 2014 9:34 196.887
8 18 2014 9:35 196.835
8 18 2014 9:36 196.81
8 18 2014 9:37 196.81
8 18 2014 9:38 196.81
但是我收到一个错误提示
值错误:解压的值太多。
我在网上读到,当您的变量多于要解析的列时,就会发生这种情况,但在我看来,我有 5 个变量和 5 列文本。有什么建议吗?
【问题讨论】:
-
如果您的文字看起来像您发布的那样,中间有空格,那么
np.loadtext将读取\n并尝试将其解压缩为 5 个值并失败。但是,如果这只是因为格式不正确,请进一步编辑您的问题以准确地表示它的外观。 -
不要使用
time作为变量名。您正在覆盖您正在导入的time模块。 -
你也可以使用
struct库来做这种事情,避免numpy docs.python.org/2/library/struct.html -
你检查下面的答案了吗?