String Split in Python

Thread Solved

Join Date: Jun 2009
Posts: 13
Reputation: kes_ee is an unknown quantity at this point 
Solved Threads: 0
kes_ee kes_ee is offline Offline
Newbie Poster

String Split in Python

 
0
  #1
Jul 1st, 2009
I am having a text file contaning the following:
"int Function1 (int arg1, "0|1", char * arg2);
int Function2 (int arg1, " ", char * prt1);
..."
I need to open this file, split the each word and store it as global variable. I did this in Perl but now I need it in Python. I am new to Python. Give some idea to acheive this in Python.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 495
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: String Split in Python

 
0
  #2
Jul 1st, 2009
If by each "word", you mean strictly at every space, then you could read the file line by line, and perform this on every line:
  1. fh = open('filename', 'r')
  2. for line in fh.readlines():
  3. # use 'xreadlines()' if the file is large and you just want an iterator
  4. words = line.split(' ')
  5. # returns an array of every item between the spaces
  6. # do something else...
  7. fh.close()
The 'words' list would look like this on the first line in that file:
  1. ['int', 'Function1', '(int', 'arg1,', '"0|1",', 'char', '*', 'arg2);']
Notice that some indices still have the commas, brackets, etc in them because they didn't have spaces in between. Is this ok or did you mean "words" as in a list of the function type, name, and the arguments not broken up like split(' ') did? Give an example of what you want the split-up line's list to look like.
Last edited by shadwickman; Jul 1st, 2009 at 2:56 am.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 13
Reputation: kes_ee is an unknown quantity at this point 
Solved Threads: 0
kes_ee kes_ee is offline Offline
Newbie Poster

Re: String Split in Python

 
0
  #3
Jul 1st, 2009
I need to take somthing like,
int, Function1, int arg1, 0|1, char * arg2 ... etc.
and similarly I need to do the same for all the lines in file and store it in somthing like global array. This varaibles will be used in some other function.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 495
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: String Split in Python

 
0
  #4
Jul 1st, 2009
You can incorporate this easily into your script as a function, etc.
  1. # split at the first bracket - the function, and the arguments
  2. func, args = f.split("(")
  3. # split the function into its type and name
  4. func = func.split(" ")
  5. # split the arguments at each comma
  6. args = args.split(",")
  7. # get rid of ending bracket and semicolon from last arg
  8. args[-1] = args[-1][:2]
  9. # put both lists together
  10. result = func + args
  11. #strip any leading or trailing whitespace from the items
  12. result = [item.strip() for item in result]
  13. # remove any list indices that are blank strings
  14. result = filter(lambda x: x != "", result)
  15.  
  16. """ my result on the first line in your file:
  17. ['int', 'Function1', 'int arg1', '"0|1"', 'char * arg2']
  18. """
That's pretty well commented for you to understand. The last little strip() part is because when split at a comma, the following argument would start with a space. By calling strip() on all the resulting items, we ensure that none of them have excess leading or trailing whitespace. And when we split the func list at any spaces (to get the type and name), the space right after the name (before the bracket originally) made a blank index, so later on I added that filter call to weed out any blank-string indices. Tell me if you need any other clarification.
Hope that helps!
Last edited by shadwickman; Jul 1st, 2009 at 5:11 am.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 13
Reputation: kes_ee is an unknown quantity at this point 
Solved Threads: 0
kes_ee kes_ee is offline Offline
Newbie Poster

Re: String Split in Python

 
0
  #5
Jul 1st, 2009
Originally Posted by shadwickman View Post
You can incorporate this easily into your script as a function, etc.
  1. # split at the first bracket - the function, and the arguments
  2. func, args = f.split("(")
  3. # split the function into its type and name
  4. func = func.split(" ")
  5. # split the arguments at each comma
  6. args = args.split(",")
  7. # get rid of ending bracket and semicolon from last arg
  8. args[-1] = args[-1][:2]
  9. # put both lists together
  10. result = func + args
  11. #strip any leading or trailing whitespace from the items
  12. result = [item.strip() for item in result]
  13. # remove any list indices that are blank strings
  14. result = filter(lambda x: x != "", result)
  15.  
  16. """ my result on the first line in your file:
  17. ['int', 'Function1', 'int arg1', '"0|1"', 'char * arg2']
  18. """
That's pretty well commented for you to understand. The last little strip() part is because when split at a comma, the following argument would start with a space. By calling strip() on all the resulting items, we ensure that none of them have excess leading or trailing whitespace. And when we split the func list at any spaces (to get the type and name), the space right after the name (before the bracket originally) made a blank index, so later on I added that filter call to weed out any blank-string indices. Tell me if you need any other clarification.
Hope that helps!
Hi,

Thank you very much for your effort and It is working for me.
And one more thing, Can I find the number of flower brackets "()" and square brackets "[]" from the following line:
Function1 (text1) (text2) (text3) [text4]
Thanks.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,597
Reputation: Lardmeister is an unknown quantity at this point 
Solved Threads: 28
Lardmeister's Avatar
Lardmeister Lardmeister is offline Offline
Posting Virtuoso

Re: String Split in Python

 
0
  #6
Jul 1st, 2009
Use something like this:
  1. s = "Function1 (text1) (text2) (text3) [text4]"
  2.  
  3. print s.count('(')
  4. print s.count(')')
  5. print s.count('[')
  6. print s.count(']')
I upped my sanitary measures, up yours!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 3926 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC