i have a string, and i want to use the elements to convert them into attrs of a class.
@staticmethod
def impWords():
#re
tempFile = open('import.txt','r+')
tempFile1 = re.findall(r'\w+', tempFile.read())
for i in range(len(tempFile1)):
new=word(word.id=i,word.data=str(tempFile1[i]), word.points=int(tempFile1[i]+1))
Repo.words.append(word)
print str(Repo.words)
the following error pops up, how can i repair this i tried some ideas i had but it didnt worked out.
File "D:\info\F P\Lab\lab5.7\Scramble\Repository\Rep.py", line 82, in impWords
new=word(id=int(i),data=str(tempFile1[i]), points=int(tempFile1[i]+1))
TypeError: cannot concatenate 'str' and 'int' objects
If you whant to solve your problem? Just make int(tempFile1[i]) + 1, but this code is absolutely not python way.
The problem is here:
Your
tmpFile[i]
is a string. You cannot add the integer1
to a string. You can try to convert your string into an integer and add the one afterwards:So the whole line looks like this:
UPDATE: Anyway, this is probably not going to work. Consider this alternative (you have to define the word-class properly):
So if understand this is what you want
But if you want get a count of each word in file look at mapreduce algorithm