[Tutor] Individual Character Count
Kyle Babich
kyle@sent.com
Sat, 7 Sep 2002 22:42:45 UT
I'm trying (with little luck) to create a function to count how many
time an individual character appears in a file. What I have so far I
have written on patterns I've noticed but it is still extremely buggy
depending on whether the character being searched for appears as the
first character in the file, the last, both, or neither. Here it is:
####################
def InCharCount(location, character):
subj =3D file(location, "r")
body =3D subj.read()
body =3D body.split("\n")
body =3D string.join(body, "")
body =3D body.split(character)
last =3D len(body)
last =3D last - 1
char =3D 0
for each in body:
char =3D char + 1
if body[0] in [""]:
char =3D char - 1
elif body[last] in [""]:
char =3D char - 1
=20
else:
pass
return char
####################
Could someone please help me work the bugs out of this? I have a
feeling that I'm making this harder than I need to for myself again.
Thank you,
--
Kyle