python - Data and signal extraction from a file -
before begin, i'm beginner @ here in python , working on data extraction file , data need produce signal of data. don't know how explain try best explain problem , here goes. given text file like:
12 0011 15 001a 20 111e 32 8877 50 00f3 56 1000
i able read files , put them dictionary:
def dictionary(filename): d = {} f = open(filename,'r') lines in f: line = lines.split(' ',1) line[1] = line[1].replace('\n','') d[line[0]] = line[1] f.close() k in sorted(d.keys()): print 'keys:', k, '-> values:', d[k] return d
well second part, relates text file, first column represents time , second column represents data. means @ time = 15s, data 001a till time = 20s, data changes 111e. data continues same (111e) till time = 32s, data changes 8877 again. same process goes on. required extract output produced time = 15s time = 60s in interval of 1s within time. problem don't know exact method part. don't know how go next key this. have tried enumerate(d)
keeps pop out attributeerror. tried d.iteritems().next()
goes infinite loop. here goes code:
def output(d): = 0 keys = sorted(d.keys()) while <= 45: time = + 15 k in keys: if time == k: sig = d[k] else: while time != k: k = d.iteritems().next()[0] print 'time:', time, '-> signal:' sig += 1
can me? lot.
edit: better understanding, expected output below:
time: 15s -> signal: 001a time: 16s -> signal: 001a time: 17s -> signal: 001a time: 18s -> signal: 001a time: 19s -> signal: 001a time: 20s -> signal: 111e time: 21s -> signal: 111e time: 22s -> signal: 111e time: 23s -> signal: 111e ... time: 31s -> signal: 111e time: 32s -> signal: 8877 time: 33s -> signal: 8877 ... time: 49s -> signal: 8877 time: 50s -> signal: 00f3 time: 51s -> signal: 00f3 ... time: 55s -> signal: 00f3 time: 56s -> signal: 1000 time: 57s -> signal: 1000
... represents time still runs. show transition of data according text file above. output runs 60s
assuming file signals.txt
def read_signal(filename): open(filename) fh1: d = {} line in fh1: (t, s) = line.split() d[int(t)] = s in range(15,61): if in sorted(d): j = d[i] print ("time: " + str(i) + "s -> signal: " + j) read_signal("signals.txt")
Comments
Post a Comment