Error Occurs: Replace a character in a String

J

Jimbo

Hello, I am getting an error in my python script when I try to change
a character in a string. But I dont know why or what to do to fix
it?


I have commented in my code where the error occurs

Code:
def format_file(filename):

    HTML_file   = open(filename,'r')
    HTML_source = HTML_file.read()
    HTML_file.close()

    x = 0

    # Format all ID's & classes correctly
    temp_buf        = HTML_source.lower()
    class_occurence = temp_buf.count('class')
    ID_occurence    = temp_buf.count('id')

    for n in range(class_occurence):
        hit = temp_buf.find('class')
        if not hit==-1:
            temp_buf[hit] = '~' # Error: 'str' object does not support
item assignment
            x = hit+5

            # delete any spaces until we reach a letter or number
            while x<temp_buf[x]:
                if temp_buf[x] == ' ':
                    tempbuf[x]     = ''
                    HTML_source[x] = ''
                elif temp_buf[x] == '=':
                    pass
                #elif temp_buf[x] == "'" or temp_buf[x] == '"'
isalpha(temp_buf[x])
                else:
                    break
                x += 1
 
S

Steven D'Aprano

Hello, I am getting an error in my python script when I try to change a
character in a string. But I dont know why or what to do to fix
it?


I have commented in my code where the error occurs

Generally speaking, posting the actual error (not retyping it from
memory, or summarising it, or eluding to something vaguely similar) is
recommended. In this case though, your error is simple enough that you
can get away without the full traceback:

temp_buf[hit] = '~'
# Error: 'str' object does not support item assignment

The error is exactly what it says: strings don't support item assignment.

Try this in an interactive session:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

Just as it says: you can't assign to individual string items (characters).

This is because strings in Python are immutable -- once created, you
can't modify them. There are all sorts of reasons for this, which I won't
go into, but the consequence is that anytime you want to modify a string
you need to create a new one:
'aBcd'

But don't do the above! String concatenation is okay if you only do it
once or twice, but if you're doing a lot of it, your code will be
sloooooow. Best to avoid it whenever possible.

The best way to replace substrings is, not surprisingly, to use the
replace() method:
a~cd

Or break the string up into a list of words:

words = mystring.split()

or a list of characters:

chars = list(mystring)

and operate on the list. Lists, unlike strings, are mutable and so you
can assign to items.

But I see you're trying to manually parse HTML. Have you considered using
the HTML parser that comes with Python?
 
P

Patrick Maupin

Hello, I am getting an error in my python script when I try to change
a character in a string. But I dont know why or what to do to fix
it?


I have commented in my code where the error occurs

Code:
def format_file(filename):

    HTML_file   = open(filename,'r')
    HTML_source = HTML_file.read()
    HTML_file.close()

    x = 0

    # Format all ID's & classes correctly
    temp_buf        = HTML_source.lower()
    class_occurence = temp_buf.count('class')
    ID_occurence    = temp_buf.count('id')

    for n in range(class_occurence):
        hit = temp_buf.find('class')
        if not hit==-1:
            temp_buf[hit] = '~' # Error: 'str' object does not support
item assignment
            x = hit+5

            # delete any spaces until we reach a letter or number
            while x<temp_buf[x]:
                if temp_buf[x] == ' ':
                    tempbuf[x]     = ''
                    HTML_source[x] = ''
                elif temp_buf[x] == '=':
                    pass
                #elif temp_buf[x] == "'" or temp_buf[x] == '"'
isalpha(temp_buf[x])
                else:
                    break
                x += 1

In Python, a string is an immutable (unchangeable) object.

You can convert a string into a list of characters (which is not
immutable:

mylist = list(temp_buf)

and then convert it back:

temp_buf = ''.join(mylist)

but you cannot do item assignments inside the string (and lists don't
have a find method, so you cannot use find on your list). You can
maintain a parallel list and string if you want (with the same
indices).

Also, not sure what you want to accomplish with this statement:

while x<temp_buf[x]:

Do you maybe mean something like:

while x < len(temp_buf):

This whole approach (character by character processing) is pretty
inefficient (which may or may not not matter for your use). There are
several HTML parsers available for Python, as well.

HTH,
Pat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top