###前提・実現したいこと
pythonを用いて、処理したいと思っています。
環境はOS Xです。
以下のようにして、複数のファイルが同じディレクトリに存在し,そのひとつひとつに同様の処理を行い,テキストデータを一つのグラフにしようと考えていますが、
テキストデータの一行目の日時の部分が問題のようです。
一行目はsjisで書かれているようです。
エラーメッセージが出てしまいます。
どのようにしたら、エラーを解消できるでしょうか。
###発生している問題・エラーメッセージ
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x94 in position 6: invalid start byte
###該当のソースコード
python
1import matplotlib.pyplot as plt 2import numpy as np 3import os 4import sys 5 6target_dir = sys.argv[1] if len(sys.argv) >= 2 else '.' 7 8for filename in os.listdir(target_dir): 9 path_in = os.path.join(target_dir, filename) 10 if not (os.path.isfile(path_in) and path_in.endswith('.txt')): 11 continue 12 13 rfile = path_in 14 data = np.loadtxt(rfile, comments='#' ,dtype='float') 15 16 x_txt = data[:,0] 17 y_txt = data[:,3] 18 19 20 plt.figure(figsize=(8,5)) 21 plt.plot(x_txt,y_txt) 22 plt.show()
処理しようとしているテキストは以下のようなものです。
# 2016年09月09日 19時04分測定 #A #B #C #D 2.0000000E+0 -2.0000000E-7 4.2900000E-8 1.1816112E-3 1.9800000E+0 -2.0000000E-7 3.6000000E-8 1.1896633E-3 1.9600000E+0 -2.0000000E-7 3.4000000E-8 1.1655072E-3 1.9400000E+0 -2.0000000E-7 3.3000000E-8 1.1494031E-3 1.9200000E+0 -2.0000000E-7 3.2600000E-8 1.2138194E-3 1.9000000E+0 -2.0000000E-7 3.2600000E-8 1.2540795E-3 1.8800000E+0 -2.0000000E-7 3.3000000E-8 1.2943397E-3 1.8600000E+0 -2.1000000E-7 3.3900000E-8 1.1816112E-3 1.8400000E+0 -2.0000000E-7 3.5200000E-8 1.3023917E-3 1.8200000E+0 -2.1000000E-7 3.7100000E-8 1.3587560E-3 1.8000000E+0 -2.1000000E-7 3.9800000E-8 1.4392763E-3 1.7800000E+0 -2.1000000E-7 4.3200000E-8 1.5036926E-3 1.7600000E+0 -2.1000000E-7 4.7400000E-8 1.6566812E-3 1.7400000E+0 -2.1000000E-7 5.2400000E-8 1.6808373E-3 1.7200000E+0 -2.1000000E-7 5.8000000E-8 1.9465544E-3 1.7000000E+0 -2.1000000E-7 6.4400000E-8 2.1961675E-3 1.6800000E+0 -2.1000000E-7 7.1900000E-8 2.3813642E-3 1.6600000E+0 -2.1000000E-7 8.0700000E-8 2.6953935E-3 1.6400000E+0 -2.1000000E-7 9.0100000E-8 3.1946196E-3
###試したこと
テキストデータの一行目を削除すると動くようなので、以下のように書き換え、実行すると
python
1data = np.loadtxt(rfile, comments='#' ,dtype='float' ,skiprows='1')
別のエラーが発生してしまいました。
TypeError: 'str' object cannot be interpreted as an integer
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/16 02:51
2016/09/16 06:33
2016/09/16 10:05 編集
2016/09/16 11:36
2016/09/17 02:59