From: Jimbo on
Hello, I am getting an error in my python script when I try to change
a character in a string. [b]But I dont know why or what to do to fix
it?[/b]

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
[/code]
From: Steven D'Aprano on
On Sat, 10 Apr 2010 22:01:51 -0700, Jimbo wrote:

> Hello, I am getting an error in my python script when I try to change a
> character in a string. [b]But I dont know why or what to do to fix
> it?[/b]
>
> 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:

>>> s = 'abcd'
>>> s[1] = 'B'
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:

>>> s = s[0] + 'B' + s[2:]
>>> s
'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:

>>> print s.replace('B', '~')
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?



--
Steven
From: Patrick Maupin on
On Apr 11, 12:01 am, Jimbo <nill...(a)yahoo.com> wrote:
> Hello, I am getting an error in my python script when I try to change
> a character in a string. [b]But I dont know why or what to do to fix
> it?[/b]
>
> 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
> [/code]

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
 | 
Pages: 1
Prev: 2.7 beta 1
Next: Comments on PyPI