| | |
String Split in Python
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 13
Reputation:
Solved Threads: 0
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.
"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.
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:
The 'words' list would look like this on the first line in that file:
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
python Syntax (Toggle Plain Text)
fh = open('filename', 'r') for line in fh.readlines(): # use 'xreadlines()' if the file is large and you just want an iterator words = line.split(' ') # returns an array of every item between the spaces # do something else... fh.close()
Python Syntax (Toggle Plain Text)
['int', 'Function1', '(int', 'arg1,', '"0|1",', 'char', '*', 'arg2);']
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
- Hunter S. Thompson
You can incorporate this easily into your script as a function, etc.
That's pretty well commented for you to understand. The last little
Hope that helps!
python Syntax (Toggle Plain Text)
# split at the first bracket - the function, and the arguments func, args = f.split("(") # split the function into its type and name func = func.split(" ") # split the arguments at each comma args = args.split(",") # get rid of ending bracket and semicolon from last arg args[-1] = args[-1][:2] # put both lists together result = func + args #strip any leading or trailing whitespace from the items result = [item.strip() for item in result] # remove any list indices that are blank strings result = filter(lambda x: x != "", result) """ my result on the first line in your file: ['int', 'Function1', 'int arg1', '"0|1"', 'char * arg2'] """
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
- Hunter S. Thompson
•
•
Join Date: Jun 2009
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
You can incorporate this easily into your script as a function, etc.
That's pretty well commented for you to understand. The last littlepython Syntax (Toggle Plain Text)
# split at the first bracket - the function, and the arguments func, args = f.split("(") # split the function into its type and name func = func.split(" ") # split the arguments at each comma args = args.split(",") # get rid of ending bracket and semicolon from last arg args[-1] = args[-1][:2] # put both lists together result = func + args #strip any leading or trailing whitespace from the items result = [item.strip() for item in result] # remove any list indices that are blank strings result = filter(lambda x: x != "", result) """ my result on the first line in your file: ['int', 'Function1', 'int arg1', '"0|1"', 'char * arg2'] """strip()part is because when split at a comma, the following argument would start with a space. By callingstrip()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 thatfiltercall to weed out any blank-string indices. Tell me if you need any other clarification.
Hope that helps!
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]
Use something like this:
python Syntax (Toggle Plain Text)
s = "Function1 (text1) (text2) (text3) [text4]" print s.count('(') print s.count(')') print s.count('[') print s.count(']')
I upped my sanitary measures, up yours!
![]() |
Similar Threads
- Starting Python (Python)
- split string by spaces (Java)
- Read Binary data from file (Python)
- Something about String.split("-"); problem (Java)
- string split (C#)
Other Threads in the Python Forum
- Previous Thread: my socket programming not working properly to send data from client to server
- Next Thread: A little help
Views: 3926 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Python
application array beginner c++ c/c++ change character class client code command convert count create csv ctypes curved database dictionary django dll error examples excel exe extensions fdlib file float format framework ftp function graphics gui homework image images import input library line linux list lists logging loop loops microcontroller mouse mysql mysqldb number numbers output parse parsing path port prime processing program programming py2exe pygame pygtk pyqt python random raw_input recursion recursive redirect remote scrolledtext serial server socket ssh stdout string strings syntax table terminal text thread threading tkinter transparency tuple tutorial ubuntu unicode variable variables web windows wxpython xml






