0

I have to read data from a file. the file is like this.

CIRCUITNAME=CIRCUIT1
00.12 12/20 2.3 23.6
00.12 12/20 2.3 23.6
00.42 12/20 2.2 23.3
00.42 12/20 2.2 23.3
00.42 12/20 2.2 23.3
01.12 12/20 2.2 23.1
01.12 12/20 2.2 23.1

Now, I have coded a function which reads this and return a corresponding numpy array for every column.

def load_ci(filepath):
 fileObj=open(filepath, 'r')
 time_1=[]
 time_2=[]
 date_count=[]
 t=0
 ti=0
 d=""
 #da=0
 loadCurrent_1=[]
 surfaceTemp_1=[]
 loadCurrent_2=[]
 surfaceTemp_2=[]
 ambient=[]
 read=0
 for line in fileObj:
    if not line.strip():
        continue    
    if read==1:
        if '[AMBIENT]' in line:
            read=3
            continue
        elif  'CIRCUITNAME=CIRCUIT2' in line: read=2
        else:
            if line!='\n' and '[CIRCUIT2]' not in line:
                point=line.split(' ')                    
                date_count.append(point[1])
                t=(float(point[0]))
                ti=int(t)*3600+(t-int(t))*60*100
                time_1.append(ti)
                loadCurrent_1.append(float(point[2]))
                surfaceTemp_1.append(float(point[3]))
    if read==2:
        if '[AMBIENT]' in line:
            read=3
            continue
        elif  'CIRCUITNAME=CIRCUIT2' in line: read=2
        else:
            if line!='\n' and '[CIRCUIT2]' not in line:
                point=line.split(' ')
                t=(float(point[0]))
                ti=int(t)*3600+(t-int(t))*60*100
                time_2.append(ti)
                loadCurrent_2.append(float(point[2]))
                surfaceTemp_2.append(float(point[3]))
    if read==3:
        if line!='\n':
            point=line.split(' ')
            ambient.append(float(point[2]))
    if 'CIRCUITNAME=CIRCUIT1' in line: read=1
return  np.array(loadCurrent_1), np.array(surfaceTemp_1),np.array(loadCurrent_2),np.array(surfaceTemp_2),np.array(ambient),np.array(time_1),np.array(time_2),np.array(date_count)

a= load_ci("2_Horizontal_Drilling_800mm_20121221_001235.ci") print a

But, I could not able to save the month-date as floats because of '/' in between months and dates. I need to save the month-date like '12.05' which means 12th of May. Then when the next day comes '12.05' becomes '12.06' and I have to save the time values accordingly by adding hours for a new day. But I could not able to save my date as a float value. Please give me some suggestions or any other way to resolve this issue.

MattDMo
  • 90,104
  • 20
  • 213
  • 210

1 Answers1

0

You probably want to use datetime as it will make it a lot easier to iterate over days etc. and switch between date formats:

https://docs.python.org/2/library/datetime.html

Having a look there might help to get started:

Parse date string and change format

You first need to parse your dates, then you can play with them as you wish and finally get them back as string or float

Community
  • 1
  • 1
etna
  • 965
  • 5
  • 13
  • I used the program like this to save my date and time and appended this for future comparison with the previous values to an array. But it doesn't work. date_count=[] d=datetime.datetime.strptime(point[0]+" "+point[1],'%H.%M %d/%m') date_count.append(d) – rahul_2008aust Apr 14 '14 at 08:44
  • My guess is your problem comes from the fact that you inversed day and month. You should be ok with this: `date_test = datetime.datetime.strptime("01.12 12/20", "%H.%M %m/%d")`. You can then add a day like this: `date_test + datetime.timedelta(days=1)`. Be cautious: if you don't provide a year.. as you can see it is assumed the date is in 1900... it might be an issue when adding 1 day on the 30th of the month. If you don't mind, it would best to provide the year: `datetime.datetime.strptime("01.12 12/20/2012", "%H.%M %m/%d/%Y")` – etna Apr 14 '14 at 12:08
  • Btw, to get the date back in original format as string after having added one day to the datetime object: `date_test_2 = date_test + datetime.timedelta(days=1)` then `date_test_2.strftime("%H.%M %m/%d")` – etna Apr 14 '14 at 12:22
  • Yes you are right. I have already solved my problem before watching your answer. Thanks anyway for your valuable comments – rahul_2008aust Apr 14 '14 at 12:58
  • Great, you may consider marking the answer as accepted if it indeed helped and the problem is solved :) – etna Apr 14 '14 at 13:30