[SciPy-user] NumPy Newcomer Questions: Reading Data Stream & Building Array

Johannes Loehnert a.u.r.e.l.i.a.n at gmx.net
Sat Sep 9 01:59:53 CDT 2006


Hi again,

> > line = read_data()
> > mapped_vals = [DATA_MAP_7[i:i+2] for i in range(2*7, 2*35)]
>
>    DATA_MAP is a dictionary; can I find keys by slicing? If so, it would be
> [i:i+1] from bytes 12-67.

I am sorry, it was late in the evening. :-) What I meant was:

mapped_vals = [DATA_MAP_7[line[i:i+2]] for i in range(2*7, 2*35)]

i.e. take a 2-byte slice of the string "line" (slicing strings is no problem) 
and then look up the resulting 2-byte chunk in a dictionary. What I had in 
mind was something like this:


for line in scanner.readline():
    header = [ DATA_MAP_2[line[2:4], DATA_MAP_5[line[5:7]] ]
    mapped_vals = [DATA_MAP_7[line[i:i+2]] for i in range(2*7, 2*35)]
    col_array = array(header + mapped_vals)


("+" concatenates the lists).

Thinking about it, you do not need to convert it to an array right here. You 
can build a list of lists and convert it into an array as the very last step.


Johannes


More information about the SciPy-user mailing list