質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
CSV

CSV(Comma-Separated Values)はコンマで区切られた明白なテキスト値のリストです。もしくは、そのフォーマットでひとつ以上のリストを含むファイルを指します。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

Q&A

解決済

1回答

949閲覧

出力されたデータをcsvファイルに書き込みたい

eggkun

総合スコア7

CSV

CSV(Comma-Separated Values)はコンマで区切られた明白なテキスト値のリストです。もしくは、そのフォーマットでひとつ以上のリストを含むファイルを指します。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

0グッド

0クリップ

投稿2021/09/02 07:25

前提・実現したいこと

mindwave mobile2 で取得したデータをリアルタイムでグラフ化しました。その値をcsvファイルに保存したいのですが画像のようになってうまくいきません。
attentionの列もしくは行
meditationの列もしくは行
といったように各列か行に1種類のデータを書き込みたいです。
data = の部分に配列を多く書き込みすぎているとは思うのですが良い解決方法が浮かばず、調べても分からなかったので質問させていただきました。
イメージ説明

該当のソースコード

python

1 2import thinkgear 3import re 4import matplotlib.pyplot as plt 5import numpy as np 6import csv 7 8PORT = '/dev/tty.MindWaveMobile-SerialPo' 9 10attention_array = np.zeros(100) 11meditation_array = np.zeros(100) 12 13delta_array = np.zeros(100) 14theta_array = np.zeros(100) 15lowalpha_array = np.zeros(100) 16highalpha_array = np.zeros(100) 17lowbeta_array = np.zeros(100) 18highbeta_array = np.zeros(100) 19lowgamma_array = np.zeros(100) 20midgamma_array = np.zeros(100) 21 22#fig = plt.figure() 23 24def Plot(attention_array, meditation_array, delta_array, theta_array, lowalpha_array, highalpha_array, lowbeta_array, highbeta_array, lowgamma_array, midgamma_array): 25 plt.subplot(5,2,1) 26 line, = plt.plot(attention_array, color = 'red') 27 plt.ylabel("Attention") 28 29 plt.subplot(5,2,2) 30 line2, = plt.plot(meditation_array, color = 'blue') 31 plt.ylabel("Meditation") 32 33 plt.subplot(5,2,3) 34 line3, = plt.plot(delta_array, color = 'black') 35 plt.ylabel("Delta") 36 37 plt.subplot(5,2,4) 38 line4, = plt.plot(theta_array, color = 'deeppink') 39 plt.ylabel("Theta") 40 41 plt.subplot(5,2,5) 42 line5, = plt.plot(lowalpha_array, color = 'orange') 43 plt.ylabel("Lowalpha") 44 45 plt.subplot(5,2,6) 46 line6, = plt.plot(highalpha_array, color = 'red') 47 plt.ylabel("Highalpha") 48 49 plt.subplot(5,2,7) 50 line7, = plt.plot(lowbeta_array, color = 'royalblue') 51 plt.ylabel("Lowbeta") 52 53 plt.subplot(5,2,8) 54 line8, = plt.plot(highbeta_array, color = 'blue') 55 plt.ylabel("Highbeta") 56 57 plt.subplot(5,2,9) 58 line9, = plt.plot(lowgamma_array, color = 'lime') 59 plt.ylabel("Lowgamma") 60 61 plt.subplot(5,2,10) 62 line10, = plt.plot(midgamma_array, color = 'green') 63 plt.ylabel("Midgamma") 64 65 plt.pause(0.01) 66 line.remove() 67 line2.remove() 68 line3.remove() 69 line4.remove() 70 line5.remove() 71 line6.remove() 72 line7.remove() 73 line8.remove() 74 line9.remove() 75 line10.remove() 76 77for packets in thinkgear.ThinkGearProtocol(PORT).get_packets(): 78 for p in packets: 79 80 if isinstance(p, thinkgear.ThinkGearRawWaveData): 81 continue 82 83 t = str(p) 84 if t != '': 85 differencer = t[0:1] 86 if int(differencer) == 2: 87 attention = t[1:] 88 attention_array = np.append(attention_array, attention) 89 attention_array = attention_array[1:101] 90 elif int(differencer) == 3: 91 meditation = t[1:] 92 meditation_array = np.append(meditation_array, meditation) 93 meditation_array = meditation_array[1:101] 94 95 96 97 if isinstance(p, thinkgear.ThinkGearEEGPowerData): 98 buf = str(p) 99 100 idx_d0 = buf.find("delta=") 101 buf2 = buf[idx_d0+6:] 102 idx_d1 = buf2.find(",") 103 104 idx_d2 = buf.find("theta=") 105 buf3 = buf[idx_d2+6:] 106 idx_d2 = buf3.find(",") 107 108 idx_d3 = buf.find("lowalpha=") 109 buf4 = buf[idx_d3+9:] 110 idx_d3 = buf4.find(",") 111 112 idx_d4 = buf.find("highalpha=") 113 buf5 = buf[idx_d4+10:] 114 idx_d4 = buf5.find(",") 115 116 idx_d5 = buf.find("lowbeta=") 117 buf6 = buf[idx_d5+8:] 118 idx_d5 = buf6.find(",") 119 120 idx_d6 = buf.find("highbeta=") 121 buf7 = buf[idx_d6+9:] 122 idx_d6 = buf7.find(",") 123 124 idx_d7 = buf.find("lowgamma=") 125 buf8 = buf[idx_d7+9:] 126 idx_d7 = buf8.find(",") 127 128 idx_d8 = buf.find("midgamma=") 129 buf9 = buf[idx_d8+9:] 130 idx_d8 = buf9.find(")") 131 132 133 delta = int(buf2[:idx_d1]) 134 theta = int(buf3[:idx_d2]) 135 lowalpha = int(buf4[:idx_d3]) 136 highalpha = int(buf5[:idx_d4]) 137 lowbeta = int(buf6[:idx_d5]) 138 highbeta = int(buf7[:idx_d6]) 139 lowgamma = int(buf8[:idx_d7]) 140 midgamma = int(buf9[:idx_d8]) 141 142 delta_array = np.append(delta_array, delta) 143 delta_array = delta_array[1:101] 144 145 theta_array = np.append(theta_array, theta) 146 theta_array = theta_array[1:101] 147 148 lowalpha_array = np.append(lowalpha_array, lowalpha) 149 lowalpha_array = lowalpha_array[1:101] 150 151 highalpha_array = np.append(highalpha_array, highalpha) 152 highalpha_array = highalpha_array[1:101] 153 154 lowbeta_array = np.append(lowbeta_array, lowbeta) 155 lowbeta_array = lowbeta_array[1:101] 156 157 highbeta_array = np.append(highbeta_array, highbeta) 158 highbeta_array = highbeta_array[1:101] 159 160 lowgamma_array = np.append(lowgamma_array, lowgamma) 161 lowgamma_array = lowgamma_array[1:101] 162 163 midgamma_array = np.append(midgamma_array, midgamma) 164 midgamma_array = midgamma_array[1:101] 165 166 data = [[attention_array], [meditation_array], [delta_array], [theta_array], [lowalpha_array], [highalpha_array], [lowbeta_array], [highbeta_array], [lowgamma_array], [midgamma_array]] 167 with open('test.csv', 'w') as file: 168 writer = csv.writer(file, lineterminator = '\n') 169 writer.writerows(data) 170 171 172 Plot(attention_array, meditation_array, delta_array, theta_array, lowalpha_array, highalpha_array, lowbeta_array, highbeta_array, lowgamma_array, midgamma_array) 173 174 175 176

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

python

1 data = [[attention_array], [meditation_array], [delta_array], [theta_array], [lowalpha_array], [highalpha_array], [lowbeta_array], [highbeta_array], [lowgamma_array], [midgamma_array]]

python

1 data = [list(attention_array), list(meditation_array), list(delta_array), list(theta_array), list(lowalpha_array), list(highalpha_array), list(lowbeta_array), list(highbeta_array), list(lowgamma_array), list(midgamma_array)]

に変更してみてください。

投稿2021/09/02 09:11

ppaul

総合スコア24666

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

eggkun

2021/09/03 03:28

ppaul様のおかげで無事解決する事ができました。 ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問