-2

I need to get a split in hourly report .

Like today is 13th and its 5th hr,then my report should be like this:

13th 1st hr | 13th 2nd hr| 13th 3rd hr| 13th 4th hr |13th 5th hr| 

I need to get like this split in the hours.

Not able to get any idea. Can someone help?

I finally cracked it i was not asking for split in the string i was asking for hours split.

i have done as below:

x = datetime.datetime.now()
print x

k= x.strftime("%H")
print k

k would be present hour

output :: 2019-05-16 17:59:52.806000

k=17

j = int(k)
for i in range(0,j):
     if(i <=9):
        num1='0'+str(i)
        len = "start_date= {}:00:00&end_date= {}:59:59".format(num1,num1)
     else:
       num1=i
       len = "start_date= {}:00:00&end_date= {}:59:59".format(num1,num1)

output:: start_date= 00:00:00&end_date= 00:59:59

start_date= 01:00:00&end_date= 01:59:59 . .till . start_date= 17:00:00&end_date= 17:59:59

This hour split i asked for not string split.

Divya Nagandla
  • 95
  • 1
  • 13
  • 2
    What is your input and what is your output? – Perplexabot May 13 '19 at 16:15
  • It can probably be done with `re.split()` with regex or just a simple `str.split()` if you are confident in the input format. First, split the string by the `|` character, then for each of those subsets, split by spaces. The reason why I suggested using the `re` library is that you can also filter out non-numbers in the final step and account for leading and trailing spaces. – Jason K Lai May 13 '19 at 16:16
  • 1
    What is your input? Are there always spaces around pipes (`|`) or is it a varying factor in your output? – balkon16 May 13 '19 at 16:16
  • 3
    please share what you tried in code, any info about inputs and output, the question is not clear – David Sidarous May 13 '19 at 16:16
  • 1
    Possible duplicate of [Split a string by a delimiter in python](https://stackoverflow.com/questions/3475251/split-a-string-by-a-delimiter-in-python) – David Sidarous May 13 '19 at 16:21
  • Your question is not clear. First you ask for a split, but now it seem that you want to turn -- 1st hr -- into something using datetime. Please be more specific in the terms of the output desired. – Life is complex May 18 '19 at 14:58

2 Answers2

1

I am not sure I fully understand your intent. Assuming you meant this is a string such as:

"13th 1st hr | 13th 2nd hr| 13th 3rd hr| 13th 4th hr |13th 5th hr|"

You can .split() it like:

my_log = "13th 1st hr | 13th 2nd hr| 13th 3rd hr| 13th 4th hr |13th 5th hr|"
separate = my_log.split("|")
for log_item in separate:
    print(log_item)

Which prints:

13th 1st hr 
 13th 2nd hr
 13th 3rd hr
 13th 4th hr 

If you additionally would like to strip the whitespace so it is level:

my_log = "13th 1st hr | 13th 2nd hr| 13th 3rd hr| 13th 4th hr |13th 5th hr|"
separate = my_log.split("|")
for log_item in separate:
    print(log_item.strip())

Prints more cleanly:

13th 1st hr
13th 2nd hr
13th 3rd hr
13th 4th hr
13th 5th hr
Reedinationer
  • 5,661
  • 1
  • 12
  • 33
0

if you are asking about splitting strings in python you can use split()

datastr = "13th 1st hr | 13th 2nd hr"
datalist = datastr.split("|")
for d in datalist:
    print(d)