質問編集履歴
3
解決
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,168 +1,3 @@
|
|
1
|
-
|
1
|
+
解決しました!
|
2
|
-
ラインbotに対し、田中 出勤と打つとスプレッドシートに次のように表示されるようになっています。
|
3
|
-
名前 日付 出勤時間 退勤時間
|
4
|
-
田中 2021/08/03 23:37 00:00
|
5
|
-
|
6
|
-
次に田中 退勤と打つと、田中を含む行の最終行の三列目の値をpunch_outによって書き換えたいのですが、警告文が出たり出なかったりな上に、書き換えが起こらなくて困っています。
|
7
|
-
名前 日付 出勤時間 退勤時間
|
8
|
-
田中 2021/08/03 23:37 00:00
|
9
|
-
|
10
|
-
|
11
|
-
### 発生している問題・エラーメッセージ
|
12
|
-
|
13
|
-
```
|
14
|
-
SettingWithCopyWarning
|
2
|
+
ありがとうございました!SettingWithCopyWarning
|
15
|
-
A value is trying to be set on a copy of a slice from a DataFrame
|
16
|
-
```
|
17
|
-
|
18
|
-
### 該当のソースコード
|
19
|
-
df_name = df[df['名前'].str.contains(atd_name)]
|
20
|
-
df_name.iloc[-1, 3] = punch_out
|
21
|
-
以下のコードのこの部分が引っかかっているみたいです。
|
22
|
-
```python
|
23
|
-
from flask import Flask, request, abort
|
24
|
-
|
25
|
-
from linebot import (
|
26
|
-
LineBotApi, WebhookHandler
|
27
|
-
)
|
28
|
-
from linebot.exceptions import (
|
29
|
-
InvalidSignatureError
|
30
|
-
)
|
31
|
-
from linebot.models import (
|
32
|
-
MessageEvent, TextMessage, TextSendMessage,
|
33
|
-
)
|
34
|
-
|
35
|
-
import os
|
36
|
-
|
37
|
-
|
3
|
+
pandasのwarning
|
38
|
-
from datetime import datetime
|
39
|
-
import gspread
|
40
|
-
|
41
|
-
from oauth2client.service_account import ServiceAccountCredentials
|
42
|
-
|
43
|
-
|
44
|
-
def auth():
|
45
|
-
SP_CREDENTIAL_FILE = ''
|
46
|
-
SP_SCOPE = [
|
47
|
-
'https://spreadsheets.google.com/feeds',
|
48
|
-
'https://www.googleapis.com/auth/drive'
|
49
|
-
]
|
50
|
-
|
51
|
-
SP_SHEET_KEY = ''
|
52
|
-
SP_SHEET = ''
|
53
|
-
|
54
|
-
credentials = ServiceAccountCredentials.from_json_keyfile_name(SP_CREDENTIAL_FILE, SP_SCOPE)
|
55
|
-
gc = gspread.authorize(credentials)
|
56
|
-
|
57
|
-
worksheet = gc.open_by_key(SP_SHEET_KEY).worksheet(SP_SHEET)
|
58
|
-
return worksheet
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
#出勤
|
65
|
-
def punch_in():
|
66
|
-
worksheet =auth()
|
67
|
-
df = pd.DataFrame(worksheet.get_all_records())
|
68
|
-
|
69
|
-
name = atd_name
|
70
|
-
timestamp = datetime.now()
|
71
|
-
date = timestamp.strftime('%Y/%m/%d')
|
72
|
-
punch_in = timestamp.strftime('%H:%M')
|
73
|
-
|
74
|
-
df = df.append({'名前': name, '日付': date, '出勤時間': punch_in, '退勤時間': '00:00'}, ignore_index=True)
|
75
|
-
worksheet.update([df.columns.values.tolist()] + df.values.tolist())
|
76
|
-
|
77
|
-
print('出勤完了しました!')
|
78
|
-
#退勤
|
79
|
-
def punch_out():
|
80
|
-
worksheet =auth()
|
81
|
-
df = pd.DataFrame(worksheet.get_all_records())
|
82
|
-
|
83
|
-
timestamp = datetime.now()
|
84
|
-
punch_out = timestamp.strftime('%H:%M')
|
85
|
-
|
86
|
-
df_name = df[df['名前'].str.contains(atd_name)]
|
87
|
-
df_name.iloc[-1, 3] = punch_out
|
88
|
-
|
89
|
-
worksheet.update([df.columns.values.tolist()] + df.values.tolist())
|
90
|
-
print('退勤しました!')
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
app = Flask(__name__)
|
96
|
-
|
97
|
-
YOUR_CHANNEL_ACCESS_TOKEN = ''
|
98
|
-
YOUR_CHANNEL_SECRET = ''
|
99
|
-
|
100
|
-
line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN)
|
101
|
-
handler = WebhookHandler(YOUR_CHANNEL_SECRET)
|
102
|
-
|
103
|
-
|
104
|
-
@app.route("/")
|
105
|
-
def hello_world():
|
106
|
-
return "hello world"
|
107
|
-
|
108
|
-
@app.route("/callback", methods=['POST'])
|
109
|
-
def callback():
|
110
|
-
# get X-Line-Signature header value
|
111
|
-
signature = request.headers['X-Line-Signature']
|
112
|
-
|
113
|
-
# get request body as text
|
114
|
-
body = request.get_data(as_text=True)
|
115
|
-
app.logger.info("Request body: " + body)
|
116
|
-
|
117
|
-
# handle webhook body
|
118
|
-
try:
|
119
|
-
handler.handle(body, signature)
|
120
|
-
except InvalidSignatureError:
|
121
|
-
print("Invalid signature. Please check your channel access token/channel secret.")
|
122
|
-
abort(400)
|
123
|
-
|
124
|
-
return 'OK'
|
125
|
-
|
126
|
-
|
127
|
-
@handler.add(MessageEvent, message=TextMessage)
|
128
|
-
def handle_message(event):
|
129
|
-
attendance = event.message.text
|
130
|
-
atd = attendance.split()
|
131
|
-
|
132
|
-
global atd_name
|
133
|
-
atd_name = atd[0]
|
134
|
-
|
135
|
-
if '出勤' in atd:
|
136
|
-
punch_in()
|
137
|
-
line_bot_api.reply_message(
|
138
|
-
event.reply_token,
|
139
|
-
TextSendMessage(text= atd_name + '出勤完了しました!'))
|
140
|
-
|
141
|
-
elif '退勤' in atd:
|
142
|
-
punch_out()
|
143
|
-
line_bot_api.reply_message(
|
144
|
-
event.reply_token,
|
145
|
-
TextSendMessage(text= atd_name + '退勤しました!'))
|
146
|
-
else: pass
|
147
|
-
|
148
|
-
if __name__ == "__main__":
|
149
|
-
port = os.getenv("PORT")
|
150
|
-
app.run(host="0.0.0.0", port=port)
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
```
|
160
|
-
|
161
|
-
### 試したこと
|
162
|
-
```
|
163
|
-
df_name = df[df['名前'].str.contains(name)].copy()
|
164
|
-
```
|
165
|
-
上記のようにしてみましたが、退勤時間の書き換えをすることができませんでした。
|
166
|
-
### 補足情報(FW/ツールのバージョンなど)
|
167
|
-
|
168
|
-
ここにより詳細な情報を記載してください。
|
2
タイトルの変更
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
特定文字列を含む行の抽出
|
1
|
+
Python 特定文字列を含む行の抽出
|
body
CHANGED
File without changes
|
1
ソースコードを追加しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,22 +1,161 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
|
-
スプレッドシート
|
2
|
+
ラインbotに対し、田中 出勤と打つとスプレッドシートに次のように表示されるようになっています。
|
3
3
|
名前 日付 出勤時間 退勤時間
|
4
|
-
田中 2021/08/03 23:37
|
4
|
+
田中 2021/08/03 23:37 00:00
|
5
5
|
|
6
|
+
次に田中 退勤と打つと、田中を含む行の最終行の三列目の値をpunch_outによって書き換えたいのですが、警告文が出たり出なかったりな上に、書き換えが起こらなくて困っています。
|
7
|
+
名前 日付 出勤時間 退勤時間
|
8
|
+
田中 2021/08/03 23:37 00:00
|
9
|
+
|
10
|
+
|
6
11
|
### 発生している問題・エラーメッセージ
|
7
12
|
|
8
13
|
```
|
14
|
+
SettingWithCopyWarning:
|
9
15
|
A value is trying to be set on a copy of a slice from a DataFrame
|
10
16
|
```
|
11
17
|
|
12
18
|
### 該当のソースコード
|
19
|
+
df_name = df[df['名前'].str.contains(atd_name)]
|
20
|
+
df_name.iloc[-1, 3] = punch_out
|
21
|
+
以下のコードのこの部分が引っかかっているみたいです。
|
22
|
+
```python
|
23
|
+
from flask import Flask, request, abort
|
13
24
|
|
25
|
+
from linebot import (
|
26
|
+
LineBotApi, WebhookHandler
|
27
|
+
)
|
28
|
+
from linebot.exceptions import (
|
29
|
+
InvalidSignatureError
|
30
|
+
)
|
31
|
+
from linebot.models import (
|
32
|
+
MessageEvent, TextMessage, TextSendMessage,
|
33
|
+
)
|
34
|
+
|
14
|
-
|
35
|
+
import os
|
36
|
+
|
37
|
+
import pandas as pd
|
38
|
+
from datetime import datetime
|
39
|
+
import gspread
|
40
|
+
|
41
|
+
from oauth2client.service_account import ServiceAccountCredentials
|
42
|
+
|
43
|
+
|
44
|
+
def auth():
|
45
|
+
SP_CREDENTIAL_FILE = ''
|
46
|
+
SP_SCOPE = [
|
47
|
+
'https://spreadsheets.google.com/feeds',
|
48
|
+
'https://www.googleapis.com/auth/drive'
|
49
|
+
]
|
50
|
+
|
51
|
+
SP_SHEET_KEY = ''
|
15
|
-
|
52
|
+
SP_SHEET = ''
|
53
|
+
|
54
|
+
credentials = ServiceAccountCredentials.from_json_keyfile_name(SP_CREDENTIAL_FILE, SP_SCOPE)
|
55
|
+
gc = gspread.authorize(credentials)
|
56
|
+
|
57
|
+
worksheet = gc.open_by_key(SP_SHEET_KEY).worksheet(SP_SHEET)
|
58
|
+
return worksheet
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
#出勤
|
65
|
+
def punch_in():
|
66
|
+
worksheet =auth()
|
67
|
+
df = pd.DataFrame(worksheet.get_all_records())
|
68
|
+
|
69
|
+
name = atd_name
|
16
|
-
timestamp = datetime.now()
|
70
|
+
timestamp = datetime.now()
|
71
|
+
date = timestamp.strftime('%Y/%m/%d')
|
72
|
+
punch_in = timestamp.strftime('%H:%M')
|
73
|
+
|
74
|
+
df = df.append({'名前': name, '日付': date, '出勤時間': punch_in, '退勤時間': '00:00'}, ignore_index=True)
|
75
|
+
worksheet.update([df.columns.values.tolist()] + df.values.tolist())
|
76
|
+
|
77
|
+
print('出勤完了しました!')
|
78
|
+
#退勤
|
79
|
+
def punch_out():
|
80
|
+
worksheet =auth()
|
81
|
+
df = pd.DataFrame(worksheet.get_all_records())
|
82
|
+
|
83
|
+
timestamp = datetime.now()
|
17
|
-
|
84
|
+
punch_out = timestamp.strftime('%H:%M')
|
85
|
+
|
18
|
-
df_name = df[df['名前'].str.contains(
|
86
|
+
df_name = df[df['名前'].str.contains(atd_name)]
|
19
|
-
df_name.iloc[-1, 3] =
|
87
|
+
df_name.iloc[-1, 3] = punch_out
|
88
|
+
|
89
|
+
worksheet.update([df.columns.values.tolist()] + df.values.tolist())
|
90
|
+
print('退勤しました!')
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
app = Flask(__name__)
|
96
|
+
|
97
|
+
YOUR_CHANNEL_ACCESS_TOKEN = ''
|
98
|
+
YOUR_CHANNEL_SECRET = ''
|
99
|
+
|
100
|
+
line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN)
|
101
|
+
handler = WebhookHandler(YOUR_CHANNEL_SECRET)
|
102
|
+
|
103
|
+
|
104
|
+
@app.route("/")
|
105
|
+
def hello_world():
|
106
|
+
return "hello world"
|
107
|
+
|
108
|
+
@app.route("/callback", methods=['POST'])
|
109
|
+
def callback():
|
110
|
+
# get X-Line-Signature header value
|
111
|
+
signature = request.headers['X-Line-Signature']
|
112
|
+
|
113
|
+
# get request body as text
|
114
|
+
body = request.get_data(as_text=True)
|
115
|
+
app.logger.info("Request body: " + body)
|
116
|
+
|
117
|
+
# handle webhook body
|
118
|
+
try:
|
119
|
+
handler.handle(body, signature)
|
120
|
+
except InvalidSignatureError:
|
121
|
+
print("Invalid signature. Please check your channel access token/channel secret.")
|
122
|
+
abort(400)
|
123
|
+
|
124
|
+
return 'OK'
|
125
|
+
|
126
|
+
|
127
|
+
@handler.add(MessageEvent, message=TextMessage)
|
128
|
+
def handle_message(event):
|
129
|
+
attendance = event.message.text
|
130
|
+
atd = attendance.split()
|
131
|
+
|
132
|
+
global atd_name
|
133
|
+
atd_name = atd[0]
|
134
|
+
|
135
|
+
if '出勤' in atd:
|
136
|
+
punch_in()
|
137
|
+
line_bot_api.reply_message(
|
138
|
+
event.reply_token,
|
139
|
+
TextSendMessage(text= atd_name + '出勤完了しました!'))
|
140
|
+
|
141
|
+
elif '退勤' in atd:
|
142
|
+
punch_out()
|
143
|
+
line_bot_api.reply_message(
|
144
|
+
event.reply_token,
|
145
|
+
TextSendMessage(text= atd_name + '退勤しました!'))
|
146
|
+
else: pass
|
147
|
+
|
148
|
+
if __name__ == "__main__":
|
149
|
+
port = os.getenv("PORT")
|
150
|
+
app.run(host="0.0.0.0", port=port)
|
151
|
+
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
|
20
159
|
```
|
21
160
|
|
22
161
|
### 試したこと
|