import glob
import os
import pandas as pd
import pandas as pd
from datetime import datetime
from pytz import timezone
#Script to change CSV data(column'starttime', 'endtime'Changed from Unix time to Japan time)
#There is a target csv file directly under the program execution location
csv_list = os.listdir()
# !!Timely.remove()Please pull out the non-csv file with.
# !!I don't have time right now, but anyone who can fix it is welcome!
# (↑ Delete from list or not put in list except csv)
tz = timezone('Asia/Tokyo')
for csv_file in csv_list:
japan_starttime_list = []
japan_endtime_list = []
print(csv_file, "Read...")
df = pd.read_csv(csv_file)
starttime_series = df.starttime
endtime_series = df.endtime
for starttime in starttime_series:
starttime_str = str(starttime)[:10]
starttime_int = int(starttime_str)
# Unixtime -> Japan time(str) ->Store in list
utc_time = datetime.fromtimestamp(starttime_int)
japan_time = utc_time.astimezone(tz)
japan_time_str = japan_time.strftime('%H:%M:%S')
japan_starttime_list.append(japan_time_str)
#Do the same with endtime
for endtime in endtime_series:
endtime_str = str(endtime)[:10]
endtime_int = int(endtime_str)
# Unixtime -> Japan time(str) ->Store in list
utc_time = datetime.fromtimestamp(endtime_int)
japan_time = utc_time.astimezone(tz)
japan_time_str = japan_time.strftime('%H:%M:%S')
japan_endtime_list.append(japan_time_str)
#Data change in two columns of dataframe
df.starttime = japan_starttime_list
df.endtime = japan_endtime_list
#Create a new csv
df.to_csv('rev_' + csv_file)
Recommended Posts