473,466 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

46 New Member
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
3 1661
ghostdog74
511 Recognized Expert Contributor
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
eric dexter
46 New Member
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
eric dexter
46 New Member
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

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

Similar topics

7
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!
5
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, Ellipsis, or NewAxis" What does it mean?
4
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 too This causes duplicate names to be entered...
13
by: Hrvoje Voda | last post by:
How to put a specified dataset table into an array list ? Hrcko
11
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 datasource to nothing. I set the listbox datasource to...
2
by: Tany | last post by:
How can I declare function returning array of Integer pointers . Please help !!
14
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 implemented it this way in order to achieve a kind...
5
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 return type, that can hold data such as: user1, 1...
0
ntxsoft
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 glassfish 2. The problem is I have a class, name is...
2
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 convert type 'string' to 'string' I am trying...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.