Convert String Elements To Class Attributes (py 2.7)
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 =
Solution 1:
The problem is here:
int(tempFile1[i]+1)
Your tmpFile[i]
is a string. You cannot add the integer 1
to a string. You can try to convert your string into an integer and add the one afterwards:
int(tempFile1[i])+1
So the whole line looks like this:
new=word(word.id=i,word.data=str(tempFile1[i]), word.points=int(tempFile1[i])+1)
UPDATE: Anyway, this is probably not going to work. Consider this alternative (you have to define the word-class properly):
@staticmethoddefimpWords():
withopen('import.txt','r+') as f:
for i, word inenumerate(re.findall(r'\w+', f.read())):
Repo.words.append(word(id=i, data=word, points = int(word)+1))
Solution 2:
If you whant to solve your problem? Just make int(tempFile1[i]) + 1, but this code is absolutely not python way.
f = file('your_file')
ids_words = enumerate(re.findall(r'\w', f.read()))
out_mas = [word(word.id = id, word.data = data, word.points = int(data) + 1) forid, data in ids_words]
Solution 3:
So if understand this is what you want
classWord(object):
def__init__(self, id, data):
self.id = id
self.data = data
f = file('your_file')
result = [Word(id, data) forid, data inenumerate(re.findall(r'\w+', f.read()))]
But if you want get a count of each word in file look at mapreduce algorithm
Post a Comment for "Convert String Elements To Class Attributes (py 2.7)"