0

I am trying to edit my python script so that it will pull data from a database daily. Currently my script asks for a manual (raw) input of the date in the format: YYY:MM:DD:HH:MM:SS. But what I really need is for the start time to be calculated based on the current date.

This is what my script looks like now:

from datetime import datetime, timedelta
from pandas import DataFrame
import pandas as pd
from io import StringIO

starttime_str = raw_input("enter time:")  # example 2016:10:18:00:00:00
arr = starttime_str.split(':')
starttime = datetime(int(arr[0]), int(arr[1]), int(arr[2]), int(arr[3]), int(arr[4]), int(arr[5]))

But what I really need for the starttime_str to be the computer's date. So perhaps I should write a function to find the date and then calculate the time stamp? I will update the cron entry so that the script runs daily & automatically. I know I should use something like pd.tslib.Timestamp.now() but I don't need the whole string.

JAG2024
  • 3,987
  • 7
  • 29
  • 58

2 Answers2

1

I think this is what you want to do:

from datetime import datetime, timedelta
from pandas import DataFrame
import pandas as pd
from io import StringIO

starttime_str = str(datetime.today()).replace("-", ":")
arr = starttime_str.split(':')
starttime = datetime(int(arr[0]), int(arr[1]), int(arr[2]), int(arr[3]), int(arr[4]), int(arr[5]))
suryadina
  • 96
  • 1
  • 4
  • Hm I get the error: `ValueError: invalid literal for int() with base 10: '16 20'` – JAG2024 Jan 17 '17 at 04:42
  • I am sorry. I forgot there is space character in the date and hour. you can replace that space first before split starttime_str and you also need to use `float(arr[5])`: `starttime_str = str(datetime.today()).replace("-", ":").replace(" ", ":")` `arr = starttime_str.split(':')` `starttime = datetime(int(arr[0]), int(arr[1]), int(arr[2]), int(arr[3]), int(arr[4]), float(arr[5]))` – suryadina Jan 17 '17 at 06:11
1

The error pointed out in the comment section is resolved by using split() fuction in the following code

 from datetime import datetime, timedelta
 from pandas import DataFrame
 import pandas as pd
 from io import StringIO

 starttime_str = str(datetime.today()).replace("-", ":")
 arr = starttime_str.split(':')
 starttime = datetime(int(arr[0]), int(arr[1]), int(arr[2].split(' ')[0]), int(arr[2].split(' ')[1]), int(arr[3]), int(float(arr[4])))
Mahesh
  • 145
  • 8