[Tutor] How to set up an Array?

spir denis.spir at free.fr
Tue May 12 09:59:19 CEST 2009


Le Mon, 11 May 2009 19:09:30 -0700 (PDT),
nickel flipper <nickelflipper at yahoo.com> s'exprima ainsi:

> 
> Hello,
> Just getting started with Python, and have had some early, although trivial
> success. It looks like just the ticket to parse a data file.  Total noob at
> this, but really kind of overwhelmed by all the options.  Looking for some
> direction on which modules, librarys, or ? to accomplish the goal.
> 
> To date have been using conditional type statement to tease out
> information.  Now I need to match some strings, put them in an array or
> list? and read them back after X number of lines.  The linesplits are in a
> predictable pattern and padded by '-' to get eight values or arrays that
> will be joined together after a set condition is no longer true.
> 
> So from the data set below, will be looking to print out:
> RA7,OSC1,CLKI
> RA6,OSC2
> RA5,AN4,nSS1,LVDIN,RCV,RP2
> ETC.
> 
> Any tips greatly appreciated.  Thanks.
> 
> 
> sfr (key=PORTA addr=0xf80 size=1 access='rw rw rw u rw rw rw rw')
>     reset (por='xxxxxxxx' mclr='uuuuuuuu')
>     bit (names='RA7 RA6 RA5 - RA3 RA2 RA1 RA0' width='1 1 1 1 1 1 1 1')
>     bit (tag=scl names='RA' width='8')
>     bit (names='OSC1 OSC2 AN4 - AN3 AN2 AN1 AN0' width='1 1 1 1 1 1 1 1')
>     bit (names='CLKI CLKO nSS1 - VREF_PLUS VREF_MINUS C2INA C1INA' width='1
> 1 1 1 1 1 1 1') bit (names='- - LVDIN - C1INB CVREF_MINUS PMPA7 PMPA6'
> width='1 1 1 1 1 1 1 1') bit (names='- - RCV - - C2INB RP1 RP0' width='1 1
> 1 1 1 1 1 1') bit (names='- - RP2 - - - - -' width='1 1 1 1 1 1 1 1')
> 
> Here is some noob code used to pick out the RA7, RA6, RA5 etc.  Comments
> welcome.
> 
>     if line.startswith ( "    bit (names='R" ):
>         (a, pin7, pin6, pin5, pin4, pin3, pin2, pin1, pin0) = (line.split()
> + ["", ""])[:9] startpin = pin7[8:3]
>         if startpin.startswith == "RA" or "RB" or "RC" or "RD" or "RE" or
> "RF" or "RG" or "RH" or "RJ": print pin7[8:] + "," + pin6 + "," + pin5 +
> "," + pin4 + "," + pin3 + "," + pin2 + "," + pin1 + "," + pin0[:3] + "\n"
> else: pass

Hem, rather complicated. I would use a regex for once in this precise case. But forget it if you're not used to regexes, rather take the opportunity to learn python builtin strings methods better.

Anyway, as a step on the regex path, the following code

s = """\
sfr (key=PORTA addr=0xf80 size=1 access='rw rw rw u rw rw rw rw')
    reset (por='xxxxxxxx' mclr='uuuuuuuu')
    bit (names='RA7 RA6 RA5 - RA3 RA2 RA1 RA0' width='1 1 1 1 1 1 1 1')
    bit (tag=scl names='RA' width='8')
    bit (names='OSC1 OSC2 AN4 - AN3 AN2 AN1 AN0' width='1 1 1 1 1 1 1 1')
    bit (names='CLKI CLKO nSS1 - VREF_PLUS VREF_MINUS C2INA C1INA' width='1 1 1 1 1 1 1 1')
    bit (names='- - LVDIN - C1INB CVREF_MINUS PMPA7 PMPA6' width='1 1 1 1 1 1 1 1')
    bit (names='- - RCV - - C2INB RP1 RP0' width='1 1 1 1 1 1 1 1')
    bit (names='- - RP2 - - - - -' width='1 1 1 1 1 1 1 1')"""
from re import compile as Pattern
p = Pattern(r"names='((?:(?:\w+|-) ?){8})'")
r = p.findall(s)
print r

outputs

['RA7 RA6 RA5 - RA3 RA2 RA1 RA0', 'OSC1 OSC2 AN4 - AN3 AN2 AN1 AN0', 'CLKI CLKO nSS1 - VREF_PLUS VREF_MINUS C2INA C1INA', '- - LVDIN - C1INB CVREF_MINUS PMPA7 PMPA6', '- - RCV - - C2INB RP1 RP0', '- - RP2 - - - - -']

As you see the pattern is hardly legible. It matches a sequence of 8 'things' inside "names='...'. Each 'thing' is either an alphanumeric string or '-', possibly followed by a space. (Actually it's not fully correct.) We have to use twice so-called non-grouping parentheses "(?:...)". What is actually captured is inside simple (...).

Denis
------
la vita e estrany


More information about the Tutor mailing list