python - 尝试解压 simple.txt 文件时出现 ValueError : too many values to unpack,

标签 python numpy

我有这个简单的代码来读取和绘制 .txt 文件

import sys
import os
import numpy
import matplotlib.pyplot as plt
from pylab import *

exp_sum = 'exponential_sum.txt'

Term, Absolute_error, Relative_error= numpy.loadtxt(exp_sum, unpack =True)

plt.semilogy(Term,Absolute_error, 'm^-')
plt.semilogy(Term,Relative_error, 'g-')


xlabel('Number of terms N')
ylabel('Absolute and Relative approximation error')
legend(['Absolute error', 'relative error'], loc = 'upper right')
title('Testing convergence of exponential series with respect to error vs iteration terms')
show()

到目前为止工作正常,现在它在尝试绘制这个简单的文本文件时给了我这个奇怪的错误。请帮我解决这个值错误,.txt 文件也已附上。

#Term   Exponential_sum    Absolute_error       Relative_error       
0       1.0                1.71828182846        0.632120558829       
1       2.0                0.718281828459       0.264241117657       
2       2.5                0.218281828459       0.0803013970714      
3       2.66666666667      0.0516151617924      0.0189881568762      
4       2.70833333333      0.00994849512571     0.00365984682734     
5       2.71666666667      0.00161516179238     0.000594184817582    
6       2.71805555556      0.00022627290349     8.3241149288e-05     
7       2.71825396825      2.78602050767e-05    1.02491966745e-05    
8       2.71827876984      3.05861777505e-06    1.12520259784e-06    
9       2.71828152557      3.02885852843e-07    1.11425478283e-07    
10      2.71828180115      2.73126605776e-08    1.00477663102e-08    
11      2.7182818262       2.26055218988e-09    8.31610676352e-10    
12      2.71828182829      1.72876379878e-10    6.35976660213e-11    
13      2.71828182845      1.22857279905e-11    4.51966674753e-12    
14      2.71828182846      8.14903700075e-13    2.99786317792e-13    
15      2.71828182846      5.01820807131e-14    1.84609558095e-14    
16      2.71828182846      2.22044604925e-15    8.1685645175e-16     
17      2.71828182846      4.4408920985e-16     1.6337129035e-16     
18      2.71828182846      4.4408920985e-16     1.6337129035e-16     
19      2.71828182846      4.4408920985e-16     1.6337129035e-16

最佳答案

您正在尝试将四列解压缩为三个变量 - Exponential_sum 变量丢失!

关于python - 尝试解压 simple.txt 文件时出现 ValueError : too many values to unpack,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39543296/

相关文章:

python - 不同模块中的继承

python - np.multiply 如何工作?

python - 重新安排 numpy 数组

python - 有没有一种万无一失的方法可以让系统有足够的时间在运行 copytree 之前删除文件夹

python - re.findall 在 tekst 行上打印全文

python - AttributeError - 即使似乎没有属性错误

python - 如何返回多列和限制行

python - np.argsort() 的奇怪/错误输出

python - 无法重命名由 .sum() 函数创建的数据框的列名称

python - petsc4py : Creating AIJ Matrix from csc_matrix results in TypeError