Re: Using String for new List name
On Monday, 28 September 2009 18:54:09 Scott wrote: I am new to Python but I have studied hard and written a fairly big (to me) script/program. I have solved all of my problems by Googling but this one has got me stumped. I want to check a string for a substring and if it exists I want to create a new, empty list using that substring as the name of the list. For example: Let's say file1 has line1 through line100 as the first word in each line. for X in open(file1): Do a test. If true: Y = re.split( , X) Z = Y[0] # This is a string, maybe it is Line42 Z = [] # This doesn't work, I want a new, empty list created called Line42 not Z. Is there any way to do this? Yes Look at exec and eval But also look at using the string as a key in a dict. - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: Using String for new List name
Hendrik van Rooyen wrote: On Monday, 28 September 2009 18:54:09 Scott wrote: I am new to Python but I have studied hard and written a fairly big (to me) script/program. I have solved all of my problems by Googling but this one has got me stumped. I want to check a string for a substring and if it exists I want to create a new, empty list using that substring as the name of the list. For example: Let's say file1 has line1 through line100 as the first word in each line. for X in open(file1): Do a test. If true: Y = re.split( , X) Z = Y[0] # This is a string, maybe it is Line42 Z = [] # This doesn't work, I want a new, empty list created called Line42 not Z. Is there any way to do this? Yes Look at exec and eval Look. But don't touch ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: Using String for new List name
On Sep 28, 7:37 pm, Scott scott.freem...@gmail.com wrote: On Sep 28, 2:00 pm, Dave Angel da...@ieee.org wrote: Scott wrote: Thank you fine folks for getting back with your answers! So down the road I do dictname[line42].append(new stuff). (or [var] if I'm looping through the dict) Nope, you still haven't gotten it. Of course, I really don't know where you're going wrong, since you didn't use the same symbols as any of the responses you had gotten. I suspect that you meant dictname[] to be the dictionary that Duncan called values[]. On that assumption, in order to append, you'd want something like: values[line42].append(new stuff) or values[var].append(new stuff) if you happen to have a variable called var with a value of line42. You will need to get a firm grasp on the distinctions between symbol names, literals, and values. And although Python lets you blur these in some pretty bizarre ways, you haven't a chance of understanding those unless you learn how to play by the rules first. I'd suggest your first goal should be to come up with better naming conventions. And when asking questions here, try for more meaningful data than Line42 to make your point. Suppose a text file called customers.txt has on each line a name and some data. We want to initialize an (empty) list for each of those customers, and refer to it by the customer's name. At first glance we might seem to want to initialize a variable for each customer, but our program doesn't know any of the names ahead of time, so it's much better to have some form of collection. We choose a dictionary. transactions = {} with open(customers.txt) as infile: for line in infile: fields = line.split() customername = fields[0] #customer is first thing on the line transactions[customername] = [] #this is where we'll put the transactions at some later point, for this customer Now, if our program happens to have a special case for a single customer, we might have in our program something like: transactions[mayor].append(boots) But more likely, we'll be in a loop, working through another file: . for line in otherfile: fields = line.split() customername = fields[0] transaction = fields[1] transactions[customername].append(transaction) #append one transaction or interacting: name = raw_input(Customer name) trans = raw_input(transaction for that customer) transactions[name].append(trans) Dave, I'm amazed at everyone's willingness to share and teach! I will sure do the same once I have the experience. I think that one of the problems here is that I tried to make my initial question as bone simple as possible. When I first tried to explain what I was doing I was getting up to 2 pages and I thought I bet these folks don't need to read my program. They probably just need to know the one bit I'm looking for. So I deleted it all and reduced it to the 10 line example that I posted. It was then suggested that I eschew using regular expressions when not required because I used Y = re.split( , X) in my example. In my program it is actually aclLs = re.split(\s|:|/, aclS) which I think requires a regex. I just didn't want anyone to waste their time parsing the regex when it was not really germane to my actual question. The same applies to the suggestion for using meaningful variables. In the above aclLs represents (to me) access control list List-Split and aclS represents access control list String. Again, I thought X and Y, like foo and bar or spam and eggs would do for a simple example. Of course I then went and forgot the quotes around line42 and really looked confused. I was so excited to have an answer that I typed the reply without thinking it through. Not good. Don't worry though, I take no offense. I understand and welcome the advice. I don't have anyone to work with and this post is my first interaction with any person who knows programming and Python. I am but a network engineer (Cisco, Lan/Wan, firewalls, security, monitoring (this is the connection), etc.) who has never programmed. I will work on clearer communications in future posts. I'm happy for a chance to share what I am actually trying to accomplish here. I have a firewall with a static list of access-control-list (ACL) rules (about 500 rules). I also have a directory with one week of syslog output from the firewall. About 100 text files that are each about 10 to 30 MB in size. My quest, if you will, is to create a list of syslog entries, each representing a successful network connection, with each syslog entry listed under the access-list rule that allowed it. Since ACL rules can be written with a range of granularity, i.e. loose or tight, with or without Port Number, etc., their order is
Using String for new List name
I am new to Python but I have studied hard and written a fairly big (to me) script/program. I have solved all of my problems by Googling but this one has got me stumped. I want to check a string for a substring and if it exists I want to create a new, empty list using that substring as the name of the list. For example: Let's say file1 has line1 through line100 as the first word in each line. for X in open(file1): Do a test. If true: Y = re.split( , X) Z = Y[0] # This is a string, maybe it is Line42 Z = [] # This doesn't work, I want a new, empty list created called Line42 not Z. Is there any way to do this? -- http://mail.python.org/mailman/listinfo/python-list
Re: Using String for new List name
2009/9/28 Scott scott.freem...@gmail.com: I am new to Python but I have studied hard and written a fairly big (to me) script/program. I have solved all of my problems by Googling but this one has got me stumped. I want to check a string for a substring and if it exists I want to create a new, empty list using that substring as the name of the list. For example: What do you mean by as the name of the list? You cannot alter the name Z in the source code to be the content of the file, unless you do some serious magic ;) Let's say file1 has line1 through line100 as the first word in each line. for X in open(file1): Do a test. If true: Y = re.split( , X) Z = Y[0] # This is a string, maybe it is Line42 Z = [] # This doesn't work, I want a new, empty list created called Line42 not Z. Is there any way to do this? -- http://mail.python.org/mailman/listinfo/python-list -- twitter.com/olofb olofb.wordpress.com olofb.wordpress.com/tag/english -- http://mail.python.org/mailman/listinfo/python-list
Re: Using String for new List name
Scott scott.freem...@gmail.com wrote: for X in open(file1): Do a test. If true: Y = re.split( , X) Z = Y[0] # This is a string, maybe it is Line42 Z = [] # This doesn't work, I want a new, empty list created called Line42 not Z. Is there any way to do this? Use a dictionary. Also use meaningful variable names, don't use regular expressions unless you actually get some benefit from using them, and always close the file when you've finished with it. values = {} with open(file1) as f: for line in f: fields = line.split(None, 1) values[fields[0]] = [] -- http://mail.python.org/mailman/listinfo/python-list
Re: Using String for new List name
Scott wrote: I am new to Python but I have studied hard and written a fairly big (to me) script/program. I have solved all of my problems by Googling but this one has got me stumped. I want to check a string for a substring and if it exists I want to create a new, empty list using that substring as the name of the list. For example: Let's say file1 has line1 through line100 as the first word in each line. for X in open(file1): Do a test. If true: Y = re.split( , X) Z = Y[0] # This is a string, maybe it is Line42 Z = [] # This doesn't work, I want a new, empty list created called Line42 not Z. Is there any way to do this? Assuming you made this work, and had a new variable called Line42, how would you know it was called Line42 in the rest of your program? What you could do is create a dict and have the key set to the new name, e.g.: new_names = {} for X in open(file1); Do a test. if True: Y = X.split( ) new_names[Y[0]] = [] then in the rest of your program you can refer to the keys in new_names: for var in new_names: item = new_names[var] do_something_with(item) Hope this helps! ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Using String for new List name
Thank you fine folks for getting back with your answers! So down the road I do dictname[line42].append(new stuff). (or [var] if I'm looping through the dict) This is cool and should do the trick! -Scott Freemire disclosure - Ok, I'm new to *any* language. I've been teaching myself for about 3 months with Learning Python, 3rd Edition and I think it's going well! Of course I picked something way too complicated for a first try. Thanks again! -- http://mail.python.org/mailman/listinfo/python-list
Re: Using String for new List name
2009/9/28 Scott scott.freem...@gmail.com: Thank you fine folks for getting back with your answers! So down the road I do dictname[line42].append(new stuff). (or [var] if I'm looping through the dict) This is cool and should do the trick! -Scott Freemire disclosure - Ok, I'm new to *any* language. I've been teaching myself for about 3 months with Learning Python, 3rd Edition and I think it's going well! Of course I picked something way too complicated for a first try. Thanks again! Good luck! -- http://mail.python.org/mailman/listinfo/python-list -- twitter.com/olofb olofb.wordpress.com olofb.wordpress.com/tag/english -- http://mail.python.org/mailman/listinfo/python-list
Re: Using String for new List name
Scott wrote: Thank you fine folks for getting back with your answers! So down the road I do dictname[line42].append(new stuff). (or [var] if I'm looping through the dict) This is cool and should do the trick! -Scott Freemire disclosure - Ok, I'm new to *any* language. I've been teaching myself for about 3 months with Learning Python, 3rd Edition and I think it's going well! Of course I picked something way too complicated for a first try. Thanks again! That should actually be dictname[line42].append(new stuff). Notice the quotes around line42. Good luck! Python is a fine language, I hope you like it. ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Using String for new List name
Scott wrote: Thank you fine folks for getting back with your answers! So down the road I do dictname[line42].append(new stuff). The keys are strings, so dictname['line42'].append(new stuff) or for key in dictname.keys(): ... dictname[key] tjr -- http://mail.python.org/mailman/listinfo/python-list
Re: Using String for new List name
That should actually be dictname[line42].append(new stuff). Notice the quotes around line42. Good luck! Python is a fine language, I hope you like it. ~Ethan~ Doh. I sent it before my type, fail, fix cycle had taken place. Got it. Thanks again all! -- http://mail.python.org/mailman/listinfo/python-list
Re: Using String for new List name
Scott wrote: Thank you fine folks for getting back with your answers! So down the road I do dictname[line42].append(new stuff). (or [var] if I'm looping through the dict) Nope, you still haven't gotten it. Of course, I really don't know where you're going wrong, since you didn't use the same symbols as any of the responses you had gotten. I suspect that you meant dictname[] to be the dictionary that Duncan called values[]. On that assumption, in order to append, you'd want something like: values[line42].append(new stuff) or values[var].append(new stuff) if you happen to have a variable called var with a value of line42. You will need to get a firm grasp on the distinctions between symbol names, literals, and values. And although Python lets you blur these in some pretty bizarre ways, you haven't a chance of understanding those unless you learn how to play by the rules first. I'd suggest your first goal should be to come up with better naming conventions. And when asking questions here, try for more meaningful data than Line42 to make your point. Suppose a text file called customers.txt has on each line a name and some data. We want to initialize an (empty) list for each of those customers, and refer to it by the customer's name. At first glance we might seem to want to initialize a variable for each customer, but our program doesn't know any of the names ahead of time, so it's much better to have some form of collection. We choose a dictionary. transactions = {} with open(customers.txt) as infile: for line in infile: fields = line.split() customername = fields[0]#customer is first thing on the line transactions[customername] = [] #this is where we'll put the transactions at some later point, for this customer Now, if our program happens to have a special case for a single customer, we might have in our program something like: transactions[mayor].append(boots) But more likely, we'll be in a loop, working through another file: . for line in otherfile: fields = line.split() customername = fields[0] transaction = fields[1] transactions[customername].append(transaction)#append one transaction or interacting: name = raw_input(Customer name) trans = raw_input(transaction for that customer) transactions[name].append(trans) -- http://mail.python.org/mailman/listinfo/python-list
Re: Using String for new List name
On Sep 28, 2:00 pm, Dave Angel da...@ieee.org wrote: Scott wrote: Thank you fine folks for getting back with your answers! So down the road I do dictname[line42].append(new stuff). (or [var] if I'm looping through the dict) Nope, you still haven't gotten it. Of course, I really don't know where you're going wrong, since you didn't use the same symbols as any of the responses you had gotten. I suspect that you meant dictname[] to be the dictionary that Duncan called values[]. On that assumption, in order to append, you'd want something like: values[line42].append(new stuff) or values[var].append(new stuff) if you happen to have a variable called var with a value of line42. You will need to get a firm grasp on the distinctions between symbol names, literals, and values. And although Python lets you blur these in some pretty bizarre ways, you haven't a chance of understanding those unless you learn how to play by the rules first. I'd suggest your first goal should be to come up with better naming conventions. And when asking questions here, try for more meaningful data than Line42 to make your point. Suppose a text file called customers.txt has on each line a name and some data. We want to initialize an (empty) list for each of those customers, and refer to it by the customer's name. At first glance we might seem to want to initialize a variable for each customer, but our program doesn't know any of the names ahead of time, so it's much better to have some form of collection. We choose a dictionary. transactions = {} with open(customers.txt) as infile: for line in infile: fields = line.split() customername = fields[0] #customer is first thing on the line transactions[customername] = [] #this is where we'll put the transactions at some later point, for this customer Now, if our program happens to have a special case for a single customer, we might have in our program something like: transactions[mayor].append(boots) But more likely, we'll be in a loop, working through another file: . for line in otherfile: fields = line.split() customername = fields[0] transaction = fields[1] transactions[customername].append(transaction) #append one transaction or interacting: name = raw_input(Customer name) trans = raw_input(transaction for that customer) transactions[name].append(trans) Dave, I'm amazed at everyone's willingness to share and teach! I will sure do the same once I have the experience. I think that one of the problems here is that I tried to make my initial question as bone simple as possible. When I first tried to explain what I was doing I was getting up to 2 pages and I thought I bet these folks don't need to read my program. They probably just need to know the one bit I'm looking for. So I deleted it all and reduced it to the 10 line example that I posted. It was then suggested that I eschew using regular expressions when not required because I used Y = re.split( , X) in my example. In my program it is actually aclLs = re.split(\s|:|/, aclS) which I think requires a regex. I just didn't want anyone to waste their time parsing the regex when it was not really germane to my actual question. The same applies to the suggestion for using meaningful variables. In the above aclLs represents (to me) access control list List-Split and aclS represents access control list String. Again, I thought X and Y, like foo and bar or spam and eggs would do for a simple example. Of course I then went and forgot the quotes around line42 and really looked confused. I was so excited to have an answer that I typed the reply without thinking it through. Not good. Don't worry though, I take no offense. I understand and welcome the advice. I don't have anyone to work with and this post is my first interaction with any person who knows programming and Python. I am but a network engineer (Cisco, Lan/Wan, firewalls, security, monitoring (this is the connection), etc.) who has never programmed. I will work on clearer communications in future posts. I'm happy for a chance to share what I am actually trying to accomplish here. I have a firewall with a static list of access-control-list (ACL) rules (about 500 rules). I also have a directory with one week of syslog output from the firewall. About 100 text files that are each about 10 to 30 MB in size. My quest, if you will, is to create a list of syslog entries, each representing a successful network connection, with each syslog entry listed under the access-list rule that allowed it. Since ACL rules can be written with a range of granularity, i.e. loose or tight, with or without Port Number, etc., their order is important. A firewall scans the rules in order, using the first successful match. I have 18 varieties of ACL rule to deal with. Furthermore Cisco sometimes