[Tutor] Nested loop of I/O tasks

Bo Li boli1285 at gmail.com
Tue Nov 24 23:42:07 CET 2009


Dear Python

I am new to Python and having questions about its usage. Currently I have to
read two .csv files INCT and INMRI which are similar to this

INCT
      NONAME 121.57 34.71 14.81 1.35 0 0 1  Cella 129.25 100.31 27.25 1.35 1
1 1  Chiasm 130.3 98.49 26.05 1.35 1 1 1  FMagnum 114.89 144.94 -15.74 1.35
1 1 1  Iz 121.57 198.52 30.76 1.35 1 1 1  LEAM 160.53 127.6 -1.14 1.35 1 1 1
LEAM 55.2 124.66 12.32 1.35 1 1 1  LPAF 180.67 128.26 -9.05 1.35 1 1 1  LTM
77.44 124.17 15.95 1.35 1 1 1  Leye 146.77 59.17 -2.63 1.35 1 0 0  Nz 121.57
34.71 14.81 1.35 1 1 1  Reye 91.04 57.59 6.98 1.35 0 1 0
INMRI
    NONAME 121.57 34.71 14.81 1.35 0 0 1  Cella 129.25 100.31 27.25 1.35 1 1
1  Chiasm 130.3 98.49 26.05 1.35 1 1 1  FMagnum 114.89 144.94 -15.74 1.35 1
1 1  Iz 121.57 198.52 30.76 1.35 1 1 1  LEAM 160.53 127.6 -1.14 1.35 1 1 1
LEAM 55.2 124.66 12.32 1.35 1 1 1  LPAF 180.67 128.26 -9.05 1.35 1 1 1  LTM
77.44 124.17 15.95 1.35 1 1 1  Leye 146.77 59.17 -2.63 1.35 1 0 0
My job is to match the name on the two files and combine the first three
attributes together. So far I tried to read two files. But when I tried to
match the pattern using nested loop, but Python stops me after 1 iteration.
Here is what I got so far.

INCT = open(' *.csv')
INMRI = open(' *.csv')

for row in INCT:
    name, x, y, z, a, b, c, d = row.split(",")
    print aaa,
    for row2 in INMRI:
        NAME, X, Y, Z, A, B, C, D = row2.split(",")
        if name == NAME:
            print aaa


The results are shown below

"NONAME" "NONAME" "Cella " "NONAME" "Chiasm" "NONAME" "FMagnum" "NONAME"
"Inion" "NONAME" "LEAM" "NONAME" "LTM" "NONAME" "Leye" "NONAME" "Nose"
"NONAME" "Nz" "NONAME" "REAM" "NONAME" "RTM" "NONAME" "Reye" "Cella"
"Chiasm" "FMagnum" "Iz" "LEAM" "LEAM" "LPAF" "LTM" "Leye" "Nz" "Reye"


I was a MATLAB user and am really confused by what happens with me. I wish
someone could help me with this intro problem and probably indicate a
convenient way for pattern matching. Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091124/143f9773/attachment.htm>


More information about the Tutor mailing list