472,328 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

re.split and returning an array question (or a list)

I apologise if I annoy in advance. This is very close to what I want but I need to return a list of instr_numbers not just one and it seems that no matter what I do I just get two items from a split and not the three I need.. What I am getting not is just the last instr number and the comment (Maybe a bug in my re or in the split) thanks in advance for the help. The .csd text file that I am using to test this is available if needed at https://sourceforge.net/project/showfiles.php?group_id=156455&package_id=202823&re lease_id=452665

Expand|Select|Wrap|Line Numbers
  1. def csdInstrumentList(from_file):                  
  2.     "Returns a list of .csd instruments and any comment lines after the instrument"
  3.     infile = open(from_file, 'r') 
  4.     temp_number = 0
  5.     for line in infile:
  6.         if 'instr' in line:
  7.             s = re.split(r' +',line,3)
  8.             instr_number = s[1]
  9.     return instr_number      
Oct 6 '06 #1
3 1553
511 Expert 256MB
there's no need to use re right?

Expand|Select|Wrap|Line Numbers
  1. def csdInstrumentList(from_file):                  
  2.     "Returns a list of .csd instruments and any comment lines after the instrument"
  3.     infile = open(from_file)
  4.     instr_number=[]
  5.     for line in infile:
  6.         if 'instr' in line:            
  7.         line = line.replace("  " ," ").replace("\t","")
  8.             s = line.split(";")                             
  9.             instr_number.append(s[0])
  10.     return instr_number
Oct 7 '06 #2
I cross posted this and almost came close to returning two lists with re but all I can get is the first number and comment. I am trying the other the replace I am thinking through though..

Expand|Select|Wrap|Line Numbers
  1.   "Returns a list of .csd instruments and any comment lines after the instrument"
  2.     infile = open(from_file, 'r') 
  3.     temp = 0
  4.     s = []
  6.     regex = r"\s*instr\s+([0-9]+)\s*(;.*)?"
  7.     test = re.compile(regex)
  8.     for line in infile:
  9.         res = test.match(line)
  10.         if res:
  11.              return [res.group(1), res.group(2)] 
Oct 7 '06 #3
thanks That was fantastic I changed it a little and probily need to strip some off of it.

Expand|Select|Wrap|Line Numbers
  2. def csdInstrumentList3(from_file):                  
  3.     "Returns a list of .csd instruments and any comment lines after the instrument"
  4.     infile = open(from_file)
  5.     instr_number=[]
  7.     for line in infile:
  8.         if 'instr' in line:            
  9.         line = line.replace("  " ," ").replace("\t","")
  10.             s = line.split(";")       
  11.             ins = s[0]               #grab the split instr number
  12.             con = ins.split(" ")        #split it again
  13.             v = int(con[1])              #convert it to an int    
  14.             instr_number.append(v) #add to the list
  15.             comments.append(s[1])
  16.     return instr_number, comments
Oct 7 '06 #4

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

Similar topics

by: brianshields | last post by:
array("a", "b", "c", "d", "e"); I want to now be able to load "b" into a variable ($var) then print it out print $var thanks!
by: shama.bell | last post by:
Hello, I am getting the following error when returning a list: return tbl "IndexError: each subindex must be either a slice, an integer,...
by: jonny | last post by:
Hey I need to search an array list to remove duplicate Data The list is populated by a search function that enters in a name if the it is told...
by: Hrvoje Voda | last post by:
How to put a specified dataset table into an array list ? Hrcko
by: Zorpiedoman | last post by:
The problem is this: I have a list box. I set an array list as the datasource. I remove an item from the array list. I set the listbox...
by: Tany | last post by:
How can I declare function returning array of Integer pointers . Please help !!
by: Fabian Steiner | last post by:
Hello! I have got a Python "Device" Object which has got a attribute (list) called children which my contain several other "Device" objects. I...
by: admin | last post by:
I have a class that has a function that should query a database and return a list of usernames as well as their id. What type should I use as a...
by: ntxsoft | last post by:
Hello everybody, I have a small problem while returning array from web service. Firstly I'm new at java web services and I'm using netbeans 6 with...
by: AMP | last post by:
Hello, I am trying to split an Array into another array. Each value in the array has tab delimited strings. I am getting: Cannot implicitly...
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.