0

さて、私は単純な等高線図をプロットするためにこの単純化されたコードを持っています:

try:
   header = input("\nEnter the number of rows to skip for the text file's header: ") 
except (NameError, SyntaxError, TypeError, ValueError) as e: 
   print 'This is not a valid integer. This program will be terminated.'
   sys.exit(0)

z = np.genfromtxt(mapdocument.txt,skip_header= header)
(rows,cols) = z.shape

pyplot.figure()
pyplot.contour(x, y, z, 10)
pyplot.show()

skip_headerが5であると仮定します。ユーザーが5以外の値を入力した場合に、プログラムが最後にクラッシュしないようにするにはどうすればよいか知りたいです。

これは私が得たエラーです:

Traceback (most recent call last):
  File "F:\sampletask1.py", line 134, in <module>
  z = np.genfromtxt(filename,skip_header= header)
  File "C:\Python_1\lib\site-packages\numpy\lib\npyio.py", line 1560, in genfromtxt
    raise ValueError(errmsg)
ValueError: Some errors were detected !
    Line #9 (got 348 columns instead of 2)
    Line #10 (got 348 columns instead of 2)
    Line #11 (got 348 columns instead of 2)
    and so on

ありがとうございました!

4

1 に答える 1

2

np.genfromtxttryブロック内に線を入れてみてください。その後、例外をキャッチして、好きなように実行できます。

于 2012-04-26T04:25:12.807 に答える