我有一个字符串,我想使用元素将它们转换成类的属性。你知道吗
@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)
下面的错误弹出,我如何修复这个我尝试了一些想法,但没有成功。你知道吗
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
如果你不想解决你的问题?只需使int(tempFile1[i])+1,但这段代码绝对不是python的方式。你知道吗
问题在于:
你的
tmpFile[i]
是一个字符串。不能将整数1
添加到字符串中。您可以尝试将字符串转换为整数,然后再添加一个:所以整条线看起来是这样的:
更新:无论如何,这可能行不通。考虑这个替代方案(您必须正确定义单词class):
所以如果你明白这是你想要的
但如果你想得到文件中每个单词的计数,请查看mapreduce算法
相关问题 更多 >
编程相关推荐