Translating my script from perl to python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • David Deisadze
    New Member
    • Aug 2011
    • 2

    Translating my script from perl to python

    Hello, i am translating a program that I wrote in perl to python, i am currently stuck on this section and it does not work.

    Perl:
    Code:
    [while (<MAYA>)
    {
      
      my ($line) = $_;
      chomp($line);
         
      if ($line =~ /library_geometries/)
      {
        $start = 1;
      }
      
      if ($line =~ /<\/float_array>/)
      {
        $end = 1;
      }
      
      if (($start == 1) && ($end == 0))
      {
        if ($line !~ /</) 
        {
        #print GRID "$line\n";
        
        @Split = split(/ /,$line);
        push @xCoords, $Split[0];
        push @yCoords, $Split[1];
        push @zCoords, $Split[2];
        
        }
      }
    }]
    PYTHON: WHAT I DID!
    Code:
    [while Maya:
        {
        my (line) = 
        string.rstrip(line)
    
            elif line in re.search("library_geometries"):
    
            {
                start == 1
            }
    
            if (line == float.array)
            {
                end == 1
            }
    
            if ((start == 1), (end == 0))
            {
                if (line)
                    {
                        print('Line \n')
    
                        array(Split) = split(
                        array.index(Split) = split(line)
                        push array.index(xCoords), Split(0)
                        push array.index(yCoords), Split(1)
                        push array.index(zCoords), Split(2)
    
    
                              }
                        }
                    }
                }
            }
        }
    }
    ]
    Last edited by bvdet; Aug 9 '11, 02:42 PM. Reason: Fixed code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Note my comments below:
    Code:
    # Assume you are iterating on an open file object or list of strings?
    for line in Maya:
        line = line.rstrip()
        if "library_geometries" in line:
            start == 1
    
        # What is float.array?
        # float has no attribute "array"
        elif line == float.array:
            end == 1
    
        # Where is end assigned to 0?
        if start == 1 and end == 0:
            # Is this a test for "not None"?
            if line:
                print('Line \n')
    
                # Your parentheses are unbalanced
                # I can't even guess what the following code does
                # Python has no reserved word "push"
                array(Split) = split(
                    array.index(Split) = split(line)
                    push array.index(xCoords), Split(0)
                    push array.index(yCoords), Split(1)
                    push array.index(zCoords), Split(2)

    Comment

    • David Deisadze
      New Member
      • Aug 2011
      • 2

      #3
      Float array tells the program to stop searching, if it finds library.geometr ies then it continues, but when it sees float.array it tells the program to stop, push is in perl, in python its list.append

      Here is the one I just finished, but I can't seem to run it.
      Code:
      while Maya():
          import re
          {
              line in []
      
              (p) == re.compile('library_geometries')
              (m) == p.search('library_geometries')
      
              if m in (p) 
              start = 1
      
              else:
      
              p = re.compile('floar_array')
              m = p.search('float_array')
      
              if m:
              end = 1
      
              if ((start == 1), (end == 0))
              
                  else line in
              print('Line \n'
                          Split[] = split(line)
                          list.append([xCoords], Split(0)
                          list.append([yCoords]), Split(1))
                          list.append([zCoords]), Split(2))
      
      
                                }
                          }
                      }
                  }
              }
          }
      }

      Comment

      • milesmajefski
        New Member
        • Aug 2011
        • 10

        #4
        Go to python.org and read something about Python syntax.

        Comment

        Working...