简体   繁体   中英

Data and signal extraction from a file

before I begin, I'm just a beginner at here as well as in Python and I am working on something like data extraction from a file and then from the data I need to produce a signal of the data. I don't know how to explain it exactly but I will try my best to explain the problem and here goes. I was given a text file like:

12 0011
15 001a
20 111e
32 8877
50 00f3 
56 1000

I was able to read the files and put them into a dictionary:

def dictionary(filename):
 d = {}
 f = open(filename,'r')
 for lines in f:
  line = lines.split(' ',1)
  line[1] = line[1].replace('\n','')
  d[line[0]] = line[1]
 f.close()
 for k in sorted(d.keys()):
  print 'Keys:', k, '-> Values:', d[k]
 return d

Well for the second part, it relates to the text file, where the first column represents time and the second column represents the data. It means like at time = 15s, the data is 001a up till time = 20s, where the data changes to 111e. The data continues the same (111e) up till time = 32s, where the data changes to 8877 again. Same process goes on. I was required to extract the output produced from time = 15s to time = 60s in the interval of 1s within the time. The problem is I don't know the exact method to do this part. I don't know how to go to next key for this. I have tried with enumerate(d) but it keeps pop out AttributeError. I also tried d.iteritems().next() but it goes to infinite loop. Here goes my code:

def output(d):
 a = 0
 keys = sorted(d.keys())
 while a <= 45:
  time = a + 15
  for k in keys:
   if time == k:
    sig = d[k]
   else:
    while time != k:
     k = d.iteritems().next()[0]
  print 'Time:', time, '-> Signal:' sig
  a += 1

Can anyone help me? Thanks a lot.

EDIT: For better understanding, the expected output is as 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 the time still runs. This is to show the transition of the data according to the text file above. The output runs up to 60s

Assuming your file as signals.txt

def read_signal(filename):
    with open(filename) as fh1:
        d = {}
        for line in fh1:
            (t, s) = line.split()
            d[int(t)] = s
        for i in range(15,61):
            if i in sorted(d):
                j = d[i]
            print ("Time: " + str(i) + "s -> Signal: " + j)
read_signal("signals.txt")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM