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

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

P: 46
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      
  10.  
Oct 6 '06 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 511
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
  11.  
Oct 7 '06 #2

P: 46
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..
http://groups.google.com/group/comp....19bfc9c2c408f2

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 = []
  5.  
  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)] 
  12.  
Oct 7 '06 #3

P: 46
thanks That was fantastic I changed it a little and probily need to strip some off of it.

Expand|Select|Wrap|Line Numbers
  1.  
  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=[]
  6.     comments = []
  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
  17.  
  18.  
Oct 7 '06 #4

Post your reply

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