質問編集履歴
3
解決
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,335 +1,5 @@
|
|
1
|
-
|
1
|
+
解決しました!
|
2
2
|
|
3
|
-
|
3
|
+
ありがとうございました!SettingWithCopyWarning
|
4
4
|
|
5
|
-
名前 日付 出勤時間 退勤時間
|
6
|
-
|
7
|
-
田中 2021/08/03 23:37 00:00
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
次に田中 退勤と打つと、田中を含む行の最終行の三列目の値をpunch_outによって書き換えたいのですが、警告文が出たり出なかったりな上に、書き換えが起こらなくて困っています。
|
12
|
-
|
13
|
-
名前 日付 出勤時間 退勤時間
|
14
|
-
|
15
|
-
田中 2021/08/03 23:37 00:00
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
### 発生している問題・エラーメッセージ
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
```
|
26
|
-
|
27
|
-
SettingWithCopyWarning:
|
28
|
-
|
29
|
-
A value is trying to be set on a copy of a slice from a DataFrame
|
30
|
-
|
31
|
-
```
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
### 該当のソースコード
|
36
|
-
|
37
|
-
df_name = df[df['名前'].str.contains(atd_name)]
|
38
|
-
|
39
|
-
df_name.iloc[-1, 3] = punch_out
|
40
|
-
|
41
|
-
以下のコードのこの部分が引っかかっているみたいです。
|
42
|
-
|
43
|
-
```python
|
44
|
-
|
45
|
-
from flask import Flask, request, abort
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
from linebot import (
|
50
|
-
|
51
|
-
LineBotApi, WebhookHandler
|
52
|
-
|
53
|
-
)
|
54
|
-
|
55
|
-
from linebot.exceptions import (
|
56
|
-
|
57
|
-
InvalidSignatureError
|
58
|
-
|
59
|
-
)
|
60
|
-
|
61
|
-
from linebot.models import (
|
62
|
-
|
63
|
-
MessageEvent, TextMessage, TextSendMessage,
|
64
|
-
|
65
|
-
)
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
import os
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
5
|
+
pandasのwarning
|
74
|
-
|
75
|
-
from datetime import datetime
|
76
|
-
|
77
|
-
import gspread
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
from oauth2client.service_account import ServiceAccountCredentials
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
def auth():
|
88
|
-
|
89
|
-
SP_CREDENTIAL_FILE = ''
|
90
|
-
|
91
|
-
SP_SCOPE = [
|
92
|
-
|
93
|
-
'https://spreadsheets.google.com/feeds',
|
94
|
-
|
95
|
-
'https://www.googleapis.com/auth/drive'
|
96
|
-
|
97
|
-
]
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
SP_SHEET_KEY = ''
|
102
|
-
|
103
|
-
SP_SHEET = ''
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
credentials = ServiceAccountCredentials.from_json_keyfile_name(SP_CREDENTIAL_FILE, SP_SCOPE)
|
108
|
-
|
109
|
-
gc = gspread.authorize(credentials)
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
worksheet = gc.open_by_key(SP_SHEET_KEY).worksheet(SP_SHEET)
|
114
|
-
|
115
|
-
return worksheet
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
#出勤
|
128
|
-
|
129
|
-
def punch_in():
|
130
|
-
|
131
|
-
worksheet =auth()
|
132
|
-
|
133
|
-
df = pd.DataFrame(worksheet.get_all_records())
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
name = atd_name
|
138
|
-
|
139
|
-
timestamp = datetime.now()
|
140
|
-
|
141
|
-
date = timestamp.strftime('%Y/%m/%d')
|
142
|
-
|
143
|
-
punch_in = timestamp.strftime('%H:%M')
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
df = df.append({'名前': name, '日付': date, '出勤時間': punch_in, '退勤時間': '00:00'}, ignore_index=True)
|
148
|
-
|
149
|
-
worksheet.update([df.columns.values.tolist()] + df.values.tolist())
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
print('出勤完了しました!')
|
154
|
-
|
155
|
-
#退勤
|
156
|
-
|
157
|
-
def punch_out():
|
158
|
-
|
159
|
-
worksheet =auth()
|
160
|
-
|
161
|
-
df = pd.DataFrame(worksheet.get_all_records())
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
timestamp = datetime.now()
|
166
|
-
|
167
|
-
punch_out = timestamp.strftime('%H:%M')
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
df_name = df[df['名前'].str.contains(atd_name)]
|
172
|
-
|
173
|
-
df_name.iloc[-1, 3] = punch_out
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
worksheet.update([df.columns.values.tolist()] + df.values.tolist())
|
178
|
-
|
179
|
-
print('退勤しました!')
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
app = Flask(__name__)
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
YOUR_CHANNEL_ACCESS_TOKEN = ''
|
194
|
-
|
195
|
-
YOUR_CHANNEL_SECRET = ''
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN)
|
200
|
-
|
201
|
-
handler = WebhookHandler(YOUR_CHANNEL_SECRET)
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
@app.route("/")
|
208
|
-
|
209
|
-
def hello_world():
|
210
|
-
|
211
|
-
return "hello world"
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
@app.route("/callback", methods=['POST'])
|
216
|
-
|
217
|
-
def callback():
|
218
|
-
|
219
|
-
# get X-Line-Signature header value
|
220
|
-
|
221
|
-
signature = request.headers['X-Line-Signature']
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
# get request body as text
|
226
|
-
|
227
|
-
body = request.get_data(as_text=True)
|
228
|
-
|
229
|
-
app.logger.info("Request body: " + body)
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
# handle webhook body
|
234
|
-
|
235
|
-
try:
|
236
|
-
|
237
|
-
handler.handle(body, signature)
|
238
|
-
|
239
|
-
except InvalidSignatureError:
|
240
|
-
|
241
|
-
print("Invalid signature. Please check your channel access token/channel secret.")
|
242
|
-
|
243
|
-
abort(400)
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
return 'OK'
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
@handler.add(MessageEvent, message=TextMessage)
|
254
|
-
|
255
|
-
def handle_message(event):
|
256
|
-
|
257
|
-
attendance = event.message.text
|
258
|
-
|
259
|
-
atd = attendance.split()
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
global atd_name
|
264
|
-
|
265
|
-
atd_name = atd[0]
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
if '出勤' in atd:
|
270
|
-
|
271
|
-
punch_in()
|
272
|
-
|
273
|
-
line_bot_api.reply_message(
|
274
|
-
|
275
|
-
event.reply_token,
|
276
|
-
|
277
|
-
TextSendMessage(text= atd_name + '出勤完了しました!'))
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
elif '退勤' in atd:
|
282
|
-
|
283
|
-
punch_out()
|
284
|
-
|
285
|
-
line_bot_api.reply_message(
|
286
|
-
|
287
|
-
event.reply_token,
|
288
|
-
|
289
|
-
TextSendMessage(text= atd_name + '退勤しました!'))
|
290
|
-
|
291
|
-
else: pass
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
if __name__ == "__main__":
|
296
|
-
|
297
|
-
port = os.getenv("PORT")
|
298
|
-
|
299
|
-
app.run(host="0.0.0.0", port=port)
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
```
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
### 試したこと
|
322
|
-
|
323
|
-
```
|
324
|
-
|
325
|
-
df_name = df[df['名前'].str.contains(name)].copy()
|
326
|
-
|
327
|
-
```
|
328
|
-
|
329
|
-
上記のようにしてみましたが、退勤時間の書き換えをすることができませんでした。
|
330
|
-
|
331
|
-
### 補足情報(FW/ツールのバージョンなど)
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
ここにより詳細な情報を記載してください。
|
2
タイトルの変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
特定文字列を含む行の抽出
|
1
|
+
Python 特定文字列を含む行の抽出
|
test
CHANGED
File without changes
|
1
ソースコードを追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,10 +1,20 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
スプレッドシート
|
3
|
+
ラインbotに対し、田中 出勤と打つとスプレッドシートに次のように表示されるようになっています。
|
4
4
|
|
5
5
|
名前 日付 出勤時間 退勤時間
|
6
6
|
|
7
|
-
田中 2021/08/03 23:37
|
7
|
+
田中 2021/08/03 23:37 00:00
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
次に田中 退勤と打つと、田中を含む行の最終行の三列目の値をpunch_outによって書き換えたいのですが、警告文が出たり出なかったりな上に、書き換えが起こらなくて困っています。
|
12
|
+
|
13
|
+
名前 日付 出勤時間 退勤時間
|
14
|
+
|
15
|
+
田中 2021/08/03 23:37 00:00
|
16
|
+
|
17
|
+
|
8
18
|
|
9
19
|
|
10
20
|
|
@@ -14,6 +24,8 @@
|
|
14
24
|
|
15
25
|
```
|
16
26
|
|
27
|
+
SettingWithCopyWarning:
|
28
|
+
|
17
29
|
A value is trying to be set on a copy of a slice from a DataFrame
|
18
30
|
|
19
31
|
```
|
@@ -22,19 +34,285 @@
|
|
22
34
|
|
23
35
|
### 該当のソースコード
|
24
36
|
|
25
|
-
|
37
|
+
df_name = df[df['名前'].str.contains(atd_name)]
|
38
|
+
|
39
|
+
df_name.iloc[-1, 3] = punch_out
|
40
|
+
|
41
|
+
以下のコードのこの部分が引っかかっているみたいです。
|
26
42
|
|
27
43
|
```python
|
28
44
|
|
45
|
+
from flask import Flask, request, abort
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
from linebot import (
|
50
|
+
|
51
|
+
LineBotApi, WebhookHandler
|
52
|
+
|
53
|
+
)
|
54
|
+
|
55
|
+
from linebot.exceptions import (
|
56
|
+
|
57
|
+
InvalidSignatureError
|
58
|
+
|
59
|
+
)
|
60
|
+
|
61
|
+
from linebot.models import (
|
62
|
+
|
63
|
+
MessageEvent, TextMessage, TextSendMessage,
|
64
|
+
|
65
|
+
)
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
import os
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
import pandas as pd
|
74
|
+
|
75
|
+
from datetime import datetime
|
76
|
+
|
77
|
+
import gspread
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
from oauth2client.service_account import ServiceAccountCredentials
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
def auth():
|
88
|
+
|
89
|
+
SP_CREDENTIAL_FILE = ''
|
90
|
+
|
91
|
+
SP_SCOPE = [
|
92
|
+
|
93
|
+
'https://spreadsheets.google.com/feeds',
|
94
|
+
|
95
|
+
'https://www.googleapis.com/auth/drive'
|
96
|
+
|
97
|
+
]
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
SP_SHEET_KEY = ''
|
102
|
+
|
29
|
-
|
103
|
+
SP_SHEET = ''
|
104
|
+
|
105
|
+
|
106
|
+
|
30
|
-
|
107
|
+
credentials = ServiceAccountCredentials.from_json_keyfile_name(SP_CREDENTIAL_FILE, SP_SCOPE)
|
108
|
+
|
109
|
+
gc = gspread.authorize(credentials)
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
worksheet = gc.open_by_key(SP_SHEET_KEY).worksheet(SP_SHEET)
|
114
|
+
|
115
|
+
return worksheet
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
#出勤
|
128
|
+
|
129
|
+
def punch_in():
|
130
|
+
|
131
|
+
worksheet =auth()
|
132
|
+
|
133
|
+
df = pd.DataFrame(worksheet.get_all_records())
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
name = atd_name
|
138
|
+
|
31
|
-
timestamp = datetime.now()
|
139
|
+
timestamp = datetime.now()
|
140
|
+
|
32
|
-
|
141
|
+
date = timestamp.strftime('%Y/%m/%d')
|
142
|
+
|
143
|
+
punch_in = timestamp.strftime('%H:%M')
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
df = df.append({'名前': name, '日付': date, '出勤時間': punch_in, '退勤時間': '00:00'}, ignore_index=True)
|
148
|
+
|
149
|
+
worksheet.update([df.columns.values.tolist()] + df.values.tolist())
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
print('出勤完了しました!')
|
154
|
+
|
155
|
+
#退勤
|
156
|
+
|
157
|
+
def punch_out():
|
158
|
+
|
159
|
+
worksheet =auth()
|
160
|
+
|
161
|
+
df = pd.DataFrame(worksheet.get_all_records())
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
timestamp = datetime.now()
|
166
|
+
|
33
|
-
no
|
167
|
+
punch_out = timestamp.strftime('%H:%M')
|
34
|
-
|
168
|
+
|
169
|
+
|
170
|
+
|
35
|
-
df_name = df[df['名前'].str.contains(name)]
|
171
|
+
df_name = df[df['名前'].str.contains(atd_name)]
|
36
|
-
|
172
|
+
|
37
|
-
df_name.iloc[-1, 3] = no
|
173
|
+
df_name.iloc[-1, 3] = punch_out
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
worksheet.update([df.columns.values.tolist()] + df.values.tolist())
|
178
|
+
|
179
|
+
print('退勤しました!')
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
app = Flask(__name__)
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
YOUR_CHANNEL_ACCESS_TOKEN = ''
|
194
|
+
|
195
|
+
YOUR_CHANNEL_SECRET = ''
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN)
|
200
|
+
|
201
|
+
handler = WebhookHandler(YOUR_CHANNEL_SECRET)
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
@app.route("/")
|
208
|
+
|
209
|
+
def hello_world():
|
210
|
+
|
211
|
+
return "hello world"
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
@app.route("/callback", methods=['POST'])
|
216
|
+
|
217
|
+
def callback():
|
218
|
+
|
219
|
+
# get X-Line-Signature header value
|
220
|
+
|
221
|
+
signature = request.headers['X-Line-Signature']
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
# get request body as text
|
226
|
+
|
227
|
+
body = request.get_data(as_text=True)
|
228
|
+
|
229
|
+
app.logger.info("Request body: " + body)
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
# handle webhook body
|
234
|
+
|
235
|
+
try:
|
236
|
+
|
237
|
+
handler.handle(body, signature)
|
238
|
+
|
239
|
+
except InvalidSignatureError:
|
240
|
+
|
241
|
+
print("Invalid signature. Please check your channel access token/channel secret.")
|
242
|
+
|
243
|
+
abort(400)
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
return 'OK'
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
@handler.add(MessageEvent, message=TextMessage)
|
254
|
+
|
255
|
+
def handle_message(event):
|
256
|
+
|
257
|
+
attendance = event.message.text
|
258
|
+
|
259
|
+
atd = attendance.split()
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
global atd_name
|
264
|
+
|
265
|
+
atd_name = atd[0]
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
if '出勤' in atd:
|
270
|
+
|
271
|
+
punch_in()
|
272
|
+
|
273
|
+
line_bot_api.reply_message(
|
274
|
+
|
275
|
+
event.reply_token,
|
276
|
+
|
277
|
+
TextSendMessage(text= atd_name + '出勤完了しました!'))
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
elif '退勤' in atd:
|
282
|
+
|
283
|
+
punch_out()
|
284
|
+
|
285
|
+
line_bot_api.reply_message(
|
286
|
+
|
287
|
+
event.reply_token,
|
288
|
+
|
289
|
+
TextSendMessage(text= atd_name + '退勤しました!'))
|
290
|
+
|
291
|
+
else: pass
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
if __name__ == "__main__":
|
296
|
+
|
297
|
+
port = os.getenv("PORT")
|
298
|
+
|
299
|
+
app.run(host="0.0.0.0", port=port)
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
|
38
316
|
|
39
317
|
```
|
40
318
|
|