使用してCSVからWorkdayのデータの選択:は私のデータは以下のようなフォーマットされパイソン
Username, Timestamp, Text
Joe Bloggs, Thu Oct 5 09:00:00 +0000 2017, Starting work
Jane Doe, Fri Oct 7 18:00:00 +0000 2017, Finished work
Tom Smith, Sat Oct 8 04:00:00 +0000 2017, Still coding this thing
私はこのような5M行のCSVを持っている、と私は9:00-17:00月曜 - 金曜の中にちょうどそれらを抽出したいのですが。
私はダミーデータと行単位の抽出についてたくさんの投稿を読んだことがありますが、実際にはデータセット全体をフィルタリングしたいのですが、例は不完全であるか、非専門家に混乱しています。
EDIT:答えを@ivan7707へ
感謝。ここで私の完成したコードは、私は私のコードが大変間違っていたことを知っていたので、最初は何も含んでいませんでした。
import csv
from datetime import datetime
main_file = csv.DictReader(open("source.csv","rb"))
for row in main_file: #points to csv
username = row['Username']
text = row['Text']
timestamp = row['Timestamp']
#Convert timestamp to useable format
timestamp = timestamp.split()
timestamp = (timestamp[2] + "-" + timestamp[1] + "-" + timestamp[5] + " " + timestamp[3])
dt = datetime.strptime(timestamp, "%d-%b-%Y %H:%M:%S")
if dt.isoweekday() in range(1, 6): #If day is Mon-Fri
if dt.hour in range(9, 17): #If hour is 9am-5pm
output_file.writerow([username,text,timestamp]) #Save
EDIT 2(。私は分割に頼っ%のzの問題を抱えていた):会話ivan7707後
と私はコメントであったが、ここに週番号を追加するコードですデータ:毎週スクリプトの
import csv
from datetime import datetime
main_file = csv.DictReader(open("source.csv","rb"))
for row in main_file:
username = row['Username']
text = row['Text']
timestamp = row['Timestamp']
#Convert timestamp to usable format as it was erroring with %z (+0000 part)
timestamp = timestamp.split()
timestamp = (timestamp[2] + "-" + timestamp[1] + "-" + timestamp[5] + " " + timestamp[3])
dt = datetime.strptime(timestamp, "%d-%b-%Y %H:%M:%S")
#Check if timestamp is within Mon-Fri 9am-5pm
if dt.isoweekday() in range(1, 6): #Mon-Fri
if dt.hour in range(9, 17): #9am-5pm
weekday_list.append(week)
output_file.writerow([username,text,timestamp,week]) #Writes to csv
#Handy bit to iterate one week per 5 business days
elif dt.isoweekday() == 7:
if len(weekday_list) > 1:
weekday_list = []
week += 1
出力
Username, Timestamp, Text, Week,
Joe Bloggs, 06-10-2017 16:59:59, Hello World!, 1
Jane Doe, 09-10-2017 09:00:01, Hello!, 2
が含まれていない範囲内で、右の数の点に注意してはいいですね。なぜあなたはそれに撃たれていないのでしょうか、それからあなたが試みている特定の問題で使用するために戻ってきてください。私たちはお手伝いします。 –
お返事ありがとうございます。私は下のivan7707のおかげでこの問題を解決しましたが、毎週と毎月の後にcsvを保存する方法がわかっていれば、本当に役立ちます。 –