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.

Recommended Answers

All 5 Replies

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:

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()

The 'words' list would look like this on the first line in that file:

['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.

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.

You can incorporate this easily into your script as a function, etc.

# 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']
"""

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!

You can incorporate this easily into your script as a function, etc.

# 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']
"""

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.

Use something like this:

s = "Function1 (text1) (text2) (text3) [text4]"

print s.count('(')
print s.count(')')
print s.count('[')
print s.count(']')
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.