NoSocks
NoSocks

Reputation: 61

Convert a string into a time interval (?)

I am loading in strings that represent time frames with the format DD days, HH:MM:SS, and I want to create a graph in which these time frames are the expression. That means that I need to convert them into some number-like format. Interval() doesn't seem to be working. Any ideas? Should I reformat the string prior to loading to something other than DD days, HH:MM:SS so that it is more easily usable?

Upvotes: 0

Views: 934

Answers (1)

G Brown
G Brown

Reputation: 754

There are lots of ways you could go about solving this. Your idea of reformatting prior to loading sounds like a good option, since QlikView doesn't support regular expressions.

I think the best way to solve your problem is to write a simple Python script that replaces the spaces, comma, and letters with a colon. Then, you'll have a much more workable string in DD:HH:MM:SS format. Of course, this assumes you have a flat file that you can easily tweak:

import re
myreg = re.compile('(\s(day|days),\s)')    ## we'll look for " day, " and " days, "
newfilestr = ""
with open('myfile.txt', 'r') as myfile:
    for line in myfile:
        newfilestr += re.sub(myreg, ':', line)
outputf = open('fixedtimeformat.txt', 'w')
outputf.write(newfilestr)
outputf.close()

Upvotes: 1

Related Questions