By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,626 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,626 IT Pros & Developers. It's quick & easy.

can't assign to literal ERROR, looping through list

P: 8
hi,

i am trying to get this to work. it works fine so far. only when i try to loop through the list, i get an error.
any suggestions?


Expand|Select|Wrap|Line Numbers
  1. orig_file = open ("*Strata1CombinedAll.txt", "r")
  2. lines = orig_file.readlines()
  3.  
  4.  
  5.  
  6.  
  7. import fnmatch
  8. def find(seq, pattern):
  9.     pattern = pattern.lower()
  10.     for i, n in enumerate(seq):
  11.         if fnmatch.fnmatch(n.lower(), pattern):
  12.             return i
  13.             return -1
  14.  
  15. def index(seq, pattern):
  16.     result = find(seq, pattern)
  17.     if result == -1:
  18.         raise ValueError
  19.     return result
  20. def contains(seq, pattern):
  21.     return find(seq, pattern) != -1
  22.  
  23.  
  24. for "*SWD*" in lines:
  25.     begin_line = index(lines, "*SWD*")
  26.     end_line = begin_line + 4
  27.  
  28.     del lines[begin_line:end_line]
  29.  
  30.  
  31. new_text_file = open ("*test727", "w")
  32. new_text_file.writelines(lines)
  33. new_text_file.close()
Dec 2 '10 #1
Share this Question
Share on Google+
7 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
You cannot assign a value to a string literal. You must assign to an identifier. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> lines = [1,2,3,4,5]
  2. >>> for "astring" in lines:
  3. ...     print "...."
  4. Traceback (SyntaxError: can't assign to literal
  5. >>> for n in lines:
  6. ...     print n
  7. ...     
  8. 1
  9. 2
  10. 3
  11. 4
  12. 5
  13. >>> 
Dec 2 '10 #2

P: 8
okay thanks bvdet. my problem is, that i just know parts of my list element, for which i am looping.

so i tried to work my way around it, with this code:
Expand|Select|Wrap|Line Numbers
  1. orig_file = open ("*Strata1CombinedAll.txt", "r")
  2. lines = orig_file.readlines()
  3.  
  4.  
  5.  
  6.  
  7. import fnmatch
  8. def find(seq, pattern):
  9.     pattern = pattern.lower()
  10.     for i, n in enumerate(seq):
  11.         if fnmatch.fnmatch(n.lower(), pattern):
  12.             return i
  13.             return -1
  14.  
  15. def index(seq, pattern):
  16.     result = find(seq, pattern)
  17.     if result == -1:
  18.         raise ValueError
  19.     return result
  20. def contains(seq, pattern):
  21.     return find(seq, pattern) != -1
  22.  
  23.  
  24. while contains(lines, "*SWD*"):
  25.     begin_line = index(lines, "*SWD*")
  26.     end_line = begin_line + 4
  27.  
  28.     del lines[begin_line:end_line]
  29.  
  30.  
  31. new_text_file = open ("*test727", "w")
  32. new_text_file.writelines(lines)
  33. new_text_file.close()
  34.  

but i am getting an error message, that says, that the there is no index for the "begin_line". if i do it without the while loop, i do get an index for the "begin_line".

error message:
Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "*working", line 26, in <module>
  3.     end_line = begin_line + 4
  4. TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Dec 2 '10 #3

Expert 100+
P: 621
See Traversing A List in an online book. I would suggest that you traverse the list as was done in the example
Expand|Select|Wrap|Line Numbers
  1. for cheese in cheeses:
  2.     print cheese
and print each item of the list. Then check each item for the desired string, possibly using the in operator and process accordingly. Note that you should not delete from a list that you are traversing. Append the records you want to keep to a new list instead.
Dec 2 '10 #4

P: 8
well i can not traverse the list, because i just know parts of the value. in order to traverse, you need to have the exact list element value, right?

so i need a different way. any suggestions?
Dec 2 '10 #5

Expert 100+
P: 621
You either have a list or you don't. You can not have parts of a list.
Dec 2 '10 #6

P: 8
well, i do have the full list.
i want to loop through the list.

i make up an example. i want to do something with all the green fruits in there:

Expand|Select|Wrap|Line Numbers
  1. fruit_list = ['greenapples', 'blueapples', 'greenpears', 'bluepears', 'greenoranges', 'blueoranges']
  2.  
  3. for green* in fruit_list:
but it does not work, because i need to loop for the full value like greenapples.
and i am looking for a way to work around this problem
Dec 2 '10 #7

Expert 100+
P: 621
"i want to loop through the list" is the same thing as traversing a list which I covered in my first post, as well as a link on how to search the record using the in operator. If you are not going to read the posts then there is no reason to post further.
Dec 3 '10 #8

Post your reply

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