質問編集履歴
4
ミスがあったためソースコード消去
test
CHANGED
File without changes
|
test
CHANGED
@@ -157,113 +157,3 @@
|
|
157
157
|
|
158
158
|
|
159
159
|
```
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
19'01'06'16:20
|
164
|
-
|
165
|
-
ご回答いただいてからこちらで変更したソースコードの全体になります。
|
166
|
-
|
167
|
-
```python
|
168
|
-
|
169
|
-
import numpy as np
|
170
|
-
|
171
|
-
import csv
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
x = open("test.txt", "r")#x、yは同じファイル名になるように
|
176
|
-
|
177
|
-
y = open("test.txt", "r")
|
178
|
-
|
179
|
-
z = open("test2.txt", "w")#ここを任意の名前に
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
gyou = sum(1 for i in x)
|
184
|
-
|
185
|
-
last = gyou-2
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
data=[]
|
190
|
-
|
191
|
-
for i in range(22):
|
192
|
-
|
193
|
-
y.readline()
|
194
|
-
|
195
|
-
for i in range(last):
|
196
|
-
|
197
|
-
lines1 = y.readline()
|
198
|
-
|
199
|
-
data.append(lines1)
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
print(data)
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
z.writelines(data)
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
x.close()
|
212
|
-
|
213
|
-
y.close()
|
214
|
-
|
215
|
-
z.close()
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
bad_words = ['Channels', 'Samples', 'Date', 'Time', 'Y_Unit_Label',
|
220
|
-
|
221
|
-
'X_Dimension', 'X0', 'Delta_X', '***End_of_Header***', 'X_Value']
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
#テキストからcsvの作成
|
226
|
-
|
227
|
-
with open("test2.txt", "r") as oldfile:
|
228
|
-
|
229
|
-
with open("test1.csv", "w", newline='') as newfile:
|
230
|
-
|
231
|
-
for line in oldfile:
|
232
|
-
|
233
|
-
#if any(bad_word in line
|
234
|
-
|
235
|
-
# for bad_word in bad_words):←意味不明なので外しました。
|
236
|
-
|
237
|
-
# continue
|
238
|
-
|
239
|
-
line = line.replace('\t', ',')
|
240
|
-
|
241
|
-
#line = line.lstrip() ←不要なので外しました。
|
242
|
-
|
243
|
-
newfile.write(line)
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
#以下が、name.csvとして、2列目のデータのみを抽出したファイルです。
|
248
|
-
|
249
|
-
with open('test1.csv','r',) as csv_file:
|
250
|
-
|
251
|
-
reader = csv.reader(csv_file)
|
252
|
-
|
253
|
-
with open('name.txt', 'w', newline='') as f:
|
254
|
-
|
255
|
-
for row in reader:
|
256
|
-
|
257
|
-
if reader.line_num > 22:#←22行目以下は、データではないし、エラーの元となるので外しました。
|
258
|
-
|
259
|
-
f.write(row[1] + '\n')
|
260
|
-
|
261
|
-
continue
|
262
|
-
|
263
|
-
```
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
上記のソースコードですと、エラーは出ないのですがやはり要素は連結されて出力されてしまいます。
|
268
|
-
|
269
|
-
こちらでも探していますが、あまり良い方法ではないのかもしれませんね…。
|
3
ソースコード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -157,3 +157,113 @@
|
|
157
157
|
|
158
158
|
|
159
159
|
```
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
19'01'06'16:20
|
164
|
+
|
165
|
+
ご回答いただいてからこちらで変更したソースコードの全体になります。
|
166
|
+
|
167
|
+
```python
|
168
|
+
|
169
|
+
import numpy as np
|
170
|
+
|
171
|
+
import csv
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
x = open("test.txt", "r")#x、yは同じファイル名になるように
|
176
|
+
|
177
|
+
y = open("test.txt", "r")
|
178
|
+
|
179
|
+
z = open("test2.txt", "w")#ここを任意の名前に
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
gyou = sum(1 for i in x)
|
184
|
+
|
185
|
+
last = gyou-2
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
data=[]
|
190
|
+
|
191
|
+
for i in range(22):
|
192
|
+
|
193
|
+
y.readline()
|
194
|
+
|
195
|
+
for i in range(last):
|
196
|
+
|
197
|
+
lines1 = y.readline()
|
198
|
+
|
199
|
+
data.append(lines1)
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
print(data)
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
z.writelines(data)
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
x.close()
|
212
|
+
|
213
|
+
y.close()
|
214
|
+
|
215
|
+
z.close()
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
bad_words = ['Channels', 'Samples', 'Date', 'Time', 'Y_Unit_Label',
|
220
|
+
|
221
|
+
'X_Dimension', 'X0', 'Delta_X', '***End_of_Header***', 'X_Value']
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
#テキストからcsvの作成
|
226
|
+
|
227
|
+
with open("test2.txt", "r") as oldfile:
|
228
|
+
|
229
|
+
with open("test1.csv", "w", newline='') as newfile:
|
230
|
+
|
231
|
+
for line in oldfile:
|
232
|
+
|
233
|
+
#if any(bad_word in line
|
234
|
+
|
235
|
+
# for bad_word in bad_words):←意味不明なので外しました。
|
236
|
+
|
237
|
+
# continue
|
238
|
+
|
239
|
+
line = line.replace('\t', ',')
|
240
|
+
|
241
|
+
#line = line.lstrip() ←不要なので外しました。
|
242
|
+
|
243
|
+
newfile.write(line)
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
#以下が、name.csvとして、2列目のデータのみを抽出したファイルです。
|
248
|
+
|
249
|
+
with open('test1.csv','r',) as csv_file:
|
250
|
+
|
251
|
+
reader = csv.reader(csv_file)
|
252
|
+
|
253
|
+
with open('name.txt', 'w', newline='') as f:
|
254
|
+
|
255
|
+
for row in reader:
|
256
|
+
|
257
|
+
if reader.line_num > 22:#←22行目以下は、データではないし、エラーの元となるので外しました。
|
258
|
+
|
259
|
+
f.write(row[1] + '\n')
|
260
|
+
|
261
|
+
continue
|
262
|
+
|
263
|
+
```
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
上記のソースコードですと、エラーは出ないのですがやはり要素は連結されて出力されてしまいます。
|
268
|
+
|
269
|
+
こちらでも探していますが、あまり良い方法ではないのかもしれませんね…。
|
2
ソースコード修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -60,13 +60,69 @@
|
|
60
60
|
|
61
61
|
|
62
62
|
|
63
|
-
現在走らせているソースはこちらになります
|
63
|
+
現在走らせているソースはこちらになります(修正しました)
|
64
64
|
|
65
65
|
```python
|
66
66
|
|
67
|
+
import numpy as np
|
68
|
+
|
69
|
+
import csv
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
x = open("test.txt", "r")
|
74
|
+
|
75
|
+
y = open("test.txt", "r")
|
76
|
+
|
77
|
+
z = open("test1.txt", "w")#ここを任意の名前に
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
gyou = sum(1 for i in x)
|
82
|
+
|
83
|
+
last = gyou-2
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
data=[]
|
88
|
+
|
89
|
+
for i in range(22):
|
90
|
+
|
91
|
+
y.readline()
|
92
|
+
|
93
|
+
for i in range(last):
|
94
|
+
|
95
|
+
lines1 = y.readline()
|
96
|
+
|
97
|
+
data.append(lines1)
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
print(data)
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
z.writelines(data)
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
x.close()
|
110
|
+
|
111
|
+
y.close()
|
112
|
+
|
113
|
+
z.close()
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
bad_words = ['Channels', 'Samples', 'Date', 'Time', 'Y_Unit_Label',
|
118
|
+
|
119
|
+
'X_Dimension', 'X0', 'Delta_X', '***End_of_Header***', 'X_Value']
|
120
|
+
|
121
|
+
|
122
|
+
|
67
123
|
with open("test1.txt", "r") as oldfile, \
|
68
124
|
|
69
|
-
open("test
|
125
|
+
open("test.csv", "w") as newfile:
|
70
126
|
|
71
127
|
|
72
128
|
|
@@ -86,11 +142,11 @@
|
|
86
142
|
|
87
143
|
|
88
144
|
|
89
|
-
with open('test
|
145
|
+
with open('test.csv','r') as csv_file:
|
90
146
|
|
91
147
|
reader = csv.reader(csv_file)
|
92
148
|
|
93
|
-
with open('d
|
149
|
+
with open('data.txt', 'w', newline='') as f:
|
94
150
|
|
95
151
|
for row in reader:
|
96
152
|
|
@@ -98,4 +154,6 @@
|
|
98
154
|
|
99
155
|
|
100
156
|
|
157
|
+
|
158
|
+
|
101
159
|
```
|
1
ソースコードの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -57,3 +57,45 @@
|
|
57
57
|
これを省略されずに、且つテキストに保存する方法はありますでしょうか?
|
58
58
|
|
59
59
|
ご存知でしたらご教授お願い致します。
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
現在走らせているソースはこちらになります。
|
64
|
+
|
65
|
+
```python
|
66
|
+
|
67
|
+
with open("test1.txt", "r") as oldfile, \
|
68
|
+
|
69
|
+
open("test1.csv", "w") as newfile:
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
for line in oldfile:
|
74
|
+
|
75
|
+
if any(bad_word in line for bad_word in bad_words):
|
76
|
+
|
77
|
+
continue
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
line = line.replace('\t', ',')
|
82
|
+
|
83
|
+
line = line.lstrip()
|
84
|
+
|
85
|
+
newfile.write(line)
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
with open('test1.csv','r') as csv_file:
|
90
|
+
|
91
|
+
reader = csv.reader(csv_file)
|
92
|
+
|
93
|
+
with open('deta.txt', 'w', newline='') as f:
|
94
|
+
|
95
|
+
for row in reader:
|
96
|
+
|
97
|
+
f.write(row[1] + '\n')
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
```
|