import string
file = open('Text.txt')
dataArray = file.read()
file.close()
dataArray = str(dataArray)
letters = []
qString = ""
for j in dataArray:
if j.islower() == True:
qString = qString + "l"
else:
qString = qString + "u"
for i in range(0,len(dataArray)):
indexNum = qString.find("luuuluuul")
letters.append(dataArray[indexNum+4])
qString.replace(indexNum+4,"-")
print letters
I’m programming in python, and attempting to find a sequence of letters in a text file that goes 1 lowercase letter, 3 uppercase letters, 1 lowercase letter, 3 uppercase letters, 1 lowercase letter. What I have should work as far as I know, but it gives an an error “expected character buffer object”. I’m wondering if anyone can see where I’m going wrong. The error is ocuring from:
qString.replace(indexNum+4,”-“)
str.replaceis not for replacing at an index–it’s for replacing based on content.The line that gives you your error is
qString.replace(indexNum+4,"-")for that reason. The most obvious workaround is probably to slice off either side and re-join the pieces back with the new content.Growing a string by repeated appends
q = q + ...is inefficient. Most of the time what we do instead isYou do not need to
import stringhere.Using
str.findwithout checkingif result == -1is always wrong. Consider usingstr.indexinstead.Checking
if x == Trueis silly (and sometimes buggy)–just checkif x. In your case,if j.islower().You have an indentation error in the code you posted. Try to post code that is exactly the code you’ve run (and preferably self-contained, not reading files you don’t provide, etc.) to get the best answers possible.