can't assign to literal ERROR, looping through list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scharnisto
    New Member
    • Dec 2010
    • 8

    can't assign to literal ERROR, looping through list

    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?


    Code:
    orig_file = open ("*Strata1CombinedAll.txt", "r")
    lines = orig_file.readlines()
    
    
    
    
    import fnmatch
    def find(seq, pattern):
        pattern = pattern.lower()
        for i, n in enumerate(seq):
            if fnmatch.fnmatch(n.lower(), pattern):
                return i
                return -1
    
    def index(seq, pattern):
        result = find(seq, pattern)
        if result == -1:
            raise ValueError
        return result
    def contains(seq, pattern):
        return find(seq, pattern) != -1
    
    
    for "*SWD*" in lines:
        begin_line = index(lines, "*SWD*")
        end_line = begin_line + 4
        
        del lines[begin_line:end_line]
    
    
    new_text_file = open ("*test727", "w")
    new_text_file.writelines(lines)
    new_text_file.close()
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You cannot assign a value to a string literal. You must assign to an identifier. Example:
    Code:
    >>> lines = [1,2,3,4,5]
    >>> for "astring" in lines:
    ... 	print "...."
    Traceback (SyntaxError: can't assign to literal
    >>> for n in lines:
    ... 	print n
    ... 	
    1
    2
    3
    4
    5
    >>>

    Comment

    • scharnisto
      New Member
      • Dec 2010
      • 8

      #3
      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:
      Code:
      orig_file = open ("*Strata1CombinedAll.txt", "r")
      lines = orig_file.readlines()
      
      
      
      
      import fnmatch
      def find(seq, pattern):
          pattern = pattern.lower()
          for i, n in enumerate(seq):
              if fnmatch.fnmatch(n.lower(), pattern):
                  return i
                  return -1
      
      def index(seq, pattern):
          result = find(seq, pattern)
          if result == -1:
              raise ValueError
          return result
      def contains(seq, pattern):
          return find(seq, pattern) != -1
      
      
      while contains(lines, "*SWD*"):
          begin_line = index(lines, "*SWD*")
          end_line = begin_line + 4
          
          del lines[begin_line:end_line]
      
      
      new_text_file = open ("*test727", "w")
      new_text_file.writelines(lines)
      new_text_file.close()

      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:
      Code:
      Traceback (most recent call last):
        File "*working", line 26, in <module>
          end_line = begin_line + 4
      TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        See Traversing A List in an online book. I would suggest that you traverse the list as was done in the example
        Code:
        for cheese in cheeses:
            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.

        Comment

        • scharnisto
          New Member
          • Dec 2010
          • 8

          #5
          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?

          Comment

          • dwblas
            Recognized Expert Contributor
            • May 2008
            • 626

            #6
            You either have a list or you don't. You can not have parts of a list.

            Comment

            • scharnisto
              New Member
              • Dec 2010
              • 8

              #7
              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:

              Code:
              fruit_list = ['greenapples', 'blueapples', 'greenpears', 'bluepears', 'greenoranges', 'blueoranges']
              
              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

              Comment

              • dwblas
                Recognized Expert Contributor
                • May 2008
                • 626

                #8
                "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.

                Comment

                Working...