By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,583 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 IT Pros & Developers. It's quick & easy.

Translating my script from perl to python

P: 2
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:
Expand|Select|Wrap|Line Numbers
  1. [while (<MAYA>)
  2. {
  3.  
  4.   my ($line) = $_;
  5.   chomp($line);
  6.  
  7.   if ($line =~ /library_geometries/)
  8.   {
  9.     $start = 1;
  10.   }
  11.  
  12.   if ($line =~ /<\/float_array>/)
  13.   {
  14.     $end = 1;
  15.   }
  16.  
  17.   if (($start == 1) && ($end == 0))
  18.   {
  19.     if ($line !~ /</) 
  20.     {
  21.     #print GRID "$line\n";
  22.  
  23.     @Split = split(/ /,$line);
  24.     push @xCoords, $Split[0];
  25.     push @yCoords, $Split[1];
  26.     push @zCoords, $Split[2];
  27.  
  28.     }
  29.   }
  30. }]
PYTHON: WHAT I DID!
Expand|Select|Wrap|Line Numbers
  1. [while Maya:
  2.     {
  3.     my (line) = 
  4.     string.rstrip(line)
  5.  
  6.         elif line in re.search("library_geometries"):
  7.  
  8.         {
  9.             start == 1
  10.         }
  11.  
  12.         if (line == float.array)
  13.         {
  14.             end == 1
  15.         }
  16.  
  17.         if ((start == 1), (end == 0))
  18.         {
  19.             if (line)
  20.                 {
  21.                     print('Line \n')
  22.  
  23.                     array(Split) = split(
  24.                     array.index(Split) = split(line)
  25.                     push array.index(xCoords), Split(0)
  26.                     push array.index(yCoords), Split(1)
  27.                     push array.index(zCoords), Split(2)
  28.  
  29.  
  30.                           }
  31.                     }
  32.                 }
  33.             }
  34.         }
  35.     }
  36. }
  37. ]
Aug 9 '11 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Note my comments below:
Expand|Select|Wrap|Line Numbers
  1. # Assume you are iterating on an open file object or list of strings?
  2. for line in Maya:
  3.     line = line.rstrip()
  4.     if "library_geometries" in line:
  5.         start == 1
  6.  
  7.     # What is float.array?
  8.     # float has no attribute "array"
  9.     elif line == float.array:
  10.         end == 1
  11.  
  12.     # Where is end assigned to 0?
  13.     if start == 1 and end == 0:
  14.         # Is this a test for "not None"?
  15.         if line:
  16.             print('Line \n')
  17.  
  18.             # Your parentheses are unbalanced
  19.             # I can't even guess what the following code does
  20.             # Python has no reserved word "push"
  21.             array(Split) = split(
  22.                 array.index(Split) = split(line)
  23.                 push array.index(xCoords), Split(0)
  24.                 push array.index(yCoords), Split(1)
  25.                 push array.index(zCoords), Split(2)
Aug 9 '11 #2

P: 2
Float array tells the program to stop searching, if it finds library.geometries 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.
Expand|Select|Wrap|Line Numbers
  1. while Maya():
  2.     import re
  3.     {
  4.         line in []
  5.  
  6.         (p) == re.compile('library_geometries')
  7.         (m) == p.search('library_geometries')
  8.  
  9.         if m in (p) 
  10.         start = 1
  11.  
  12.         else:
  13.  
  14.         p = re.compile('floar_array')
  15.         m = p.search('float_array')
  16.  
  17.         if m:
  18.         end = 1
  19.  
  20.         if ((start == 1), (end == 0))
  21.  
  22.             else line in
  23.         print('Line \n'
  24.                     Split[] = split(line)
  25.                     list.append([xCoords], Split(0)
  26.                     list.append([yCoords]), Split(1))
  27.                     list.append([zCoords]), Split(2))
  28.  
  29.  
  30.                           }
  31.                     }
  32.                 }
  33.             }
  34.         }
  35.     }
  36. }
Aug 9 '11 #3

P: 10
Go to python.org and read something about Python syntax.
Aug 18 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.