473,382 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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:
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
3 2202
bvdet
2,851 Expert Mod 2GB
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
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
Go to python.org and read something about Python syntax.
Aug 18 '11 #4

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

Similar topics

42
by: Fred Ma | last post by:
Hello, This is not a troll posting, and I've refrained from asking because I've seen similar threads get all nitter-nattery. But I really want to make a decision on how best to invest my time....
4
by: Xah Lee | last post by:
while programing in Python, one can lookup syntax or info for keywords or modules within Python. In the command line, type python to get into the python interactive program. then type...
4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
2
by: Xah Lee | last post by:
© # -*- coding: utf-8 -*- © # Python © © # in Python, one can define a boxed set © # of data and functions, which are © # traditionally known as "class". © © # in the following, we define a...
4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # some venture into standard modules import os # print all names exported by the module print dir(os)
1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
2
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # suppose you want to fetch a webpage. from urllib import urlopen print urlopen('http://xahlee.org/Periodic_dosage_dir/_p2/russell-lecture.html').read() #...
41
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
3
by: Xah Lee | last post by:
Split File Fullpath Into Parts Xah Lee, 20051016 Often, we are given a file fullpath and we need to split it into the directory name and file name. The file name is often split into a core...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.