我想遍历startDayValStr
,endDayValStr
,startTimeValStr
,endTimeValStr
中的每个值,并将这些值用作URL字符串中的参数。我将所有其他值用作我的end
变量,即06:00:00
是startTimeValStr
值,而07:00:00
是endTimeValStr
值。
如何正确构造循环以在time_param
中使用这些变量?
我希望time_param
看起来像这样?start=05-05-2019T06:00:00Z&end=05-05-2019T07:00:00Z
这是我当前的代码:
import pandas as pd
rng = pd.date_range(pd.Timestamp('2019-05-05' + ' ' + "'06:00:00'"),periods=25, freq='H')
dtSeries = pd.Series(rng.format())
ddf = dtSeries.to_frame(name='Date')
ddf['time'] = pd.to_datetime(ddf['Date'])
dateDF = ddf['dates'] = ddf['time'].dt.date
timeDF = ddf['dates'] = ddf['time'].dt.time
startDayVal= dateDF[::2]
endDayVal = dateDF[1::2]
startTimeVal= timeDF[::2]
endTimeVal = timeDF[1::2]
startDayValStr = (startDayVal.to_string())
endDayValStr =(endDayVal.to_string())
startTimeValStr = (startTimeVal.to_string())
endTimeValStr = (endTimeVal.to_string())
for startDate, endDate, startTime, endTime in zip (startDayValStr, endDayValStr, startTimeValStr, endTimeValStr):
time_param = '?start='+ startDate +'T'+startTime + 'Z' + '&end='+ endDate + endTime + 'Z'
print time_param
答案 0 :(得分:1)
如果使用Python 3.x
,请尝试使用string.format
的date.strftime
方法:
rng = pd.date_range(pd.Timestamp('2019-05-05' + ' ' + "'06:00:00'"),
periods=25, freq='H')
for start, end in zip(rng[::2], rng[1::2]):
time_param = '?start={}&end={}'.format(start.strftime('%d-%m-%YT%H:%M:%SZ'),
end.strftime('%d-%m-%YT%H:%M:%SZ'))
print(time_param)
[出]
?start=05-05-2019T06:00:00Z&end=05-05-2019T07:00:00Z
?start=05-05-2019T08:00:00Z&end=05-05-2019T09:00:00Z
?start=05-05-2019T10:00:00Z&end=05-05-2019T11:00:00Z
?start=05-05-2019T12:00:00Z&end=05-05-2019T13:00:00Z
?start=05-05-2019T14:00:00Z&end=05-05-2019T15:00:00Z
?start=05-05-2019T16:00:00Z&end=05-05-2019T17:00:00Z
?start=05-05-2019T18:00:00Z&end=05-05-2019T19:00:00Z
?start=05-05-2019T20:00:00Z&end=05-05-2019T21:00:00Z
?start=05-05-2019T22:00:00Z&end=05-05-2019T23:00:00Z
?start=06-05-2019T00:00:00Z&end=06-05-2019T01:00:00Z
?start=06-05-2019T02:00:00Z&end=06-05-2019T03:00:00Z
?start=06-05-2019T04:00:00Z&end=06-05-2019T05:00:00Z
答案 1 :(得分:0)
通过一些字符串操作,您可以获得结果
startDayValStr = (startDayVal.to_string(index=False)).replace(' ','').split('\n')
endDayValStr =(endDayVal.to_string(index=False)).replace(' ','').split('\n')
startTimeValStr = (startTimeVal.to_string(index=False)).replace(' ','').split('\n')
endTimeValStr = (endTimeVal.to_string(index=False)).replace(' ','').split('\n')
for startDate, endDate, startTime, endTime in zip (startDayValStr, endDayValStr, startTimeValStr, endTimeValStr):
time_param = '?start='+startDate+'T'+startTime+'Z'+'&end='+endDate+endTime + 'Z'
print time_param
输出
?start=2019-05-05T06:00:00Z&end=2019-05-0507:00:00Z
?start=2019-05-05T08:00:00Z&end=2019-05-0509:00:00Z
?start=2019-05-05T10:00:00Z&end=2019-05-0511:00:00Z
?start=2019-05-05T12:00:00Z&end=2019-05-0513:00:00Z
?start=2019-05-05T14:00:00Z&end=2019-05-0515:00:00Z
?start=2019-05-05T16:00:00Z&end=2019-05-0517:00:00Z
?start=2019-05-05T18:00:00Z&end=2019-05-0519:00:00Z
?start=2019-05-05T20:00:00Z&end=2019-05-0521:00:00Z
?start=2019-05-05T22:00:00Z&end=2019-05-0523:00:00Z
?start=2019-05-06T00:00:00Z&end=2019-05-0601:00:00Z
?start=2019-05-06T02:00:00Z&end=2019-05-0603:00:00Z
?start=2019-05-06T04:00:00Z&end=2019-05-0605:00:00Z