Reputation: 477
I am working on a program to count anglicisms in German texts and everything works fine except when trying to output the results to a text file (at the bottom of the code). Here's my code so far:
from collections import Counter
import os.path
print('='*50)
print('Welcome to the ANGLICISM COUNTER 5000')
while True:
print('='*50)
lines, blanklines, sentences, words, setnum = 0,0,0,0,1
setname = input('Please enter setname: ')
listOfAnglicisms = open('anglicisms.txt').read().split()
listOfGermanWords = open('haufigste.txt').read().split()
anglicisms = []
falsepositive = []
def derivatesFromAnglicism(word):
return any([word.startswith(a) for a in listOfAnglicisms])
while os.path.isfile(str(setname+str(setnum)+".txt")) == True:
textf = open(setname+str(setnum)+'.txt')
for line in textf:
line = line.lower()
lines+=1
if line.startswith('\n'):
blanklines+=1
else:
sentences += line.count('.') + line.count('!') + line.count('?')
words += len(line.split(None))
anglicisms.extend([word for word in line.split() if derivatesFromAnglicism(word)])
anglicisms = [x for x in anglicisms if x not in listOfGermanWords]
setnum+=1
textf.close()
print('='*50)
print('Lines : ',lines)
print('Blanklines : ',blanklines)
print('Sentences : ',sentences)
print('Words: : ',words)
print('Anglicisms : ',len(anglicisms))
print('Words until anglicism : ',int(words)/len(anglicisms)-1)
print('All anglicisms :\n',Counter(anglicisms))
print('='*50)
while falsepositive != 'n':
falsepositive = input('Please enter a false positive or "n" to continue: ')
if falsepositive == 'n':
pass
else:
while falsepositive in anglicisms:
anglicisms.remove(falsepositive)
print('='*50)
print('Lines : ',lines)
print('Blanklines : ',blanklines)
print('Sentences : ',sentences)
print('Words: : ',words)
print('Anglicisms : ',len(anglicisms))
print('Words until anglicism : ',int(words)/len(anglicisms)-1)
print('All anglicisms :\n',Counter(anglicisms))
print('='*50)
results = open(setname+'.txt', 'w')
results.write(
('='*50)+
'\n'+
setname+'\n'+
'Lines : '+lines+'\n'+
'Blanklines : '+blanklines+'\n'+
'Sentences : '+sentences+'\n'+
'Words: : '+words+'\n'+
'Anglicisms : '+len(anglicisms)+'\n'+
'Words until anglicism : '+int(words)/len(anglicisms)-1+'\n'+
'All anglicisms :\n'+Counter(anglicisms)+'\n'+
('='*50)+
'\n'
)
results.close()
And this is the error I get:
Traceback (most recent call last):
File "C:\...\anglicism_counter_5000.py", line 81, in <module>
('='*50)+
TypeError: Can't convert 'int' object to str implicitly
I have tried using str() on the variables but it didn't help. Does anybody know how to use write() properly, so this error won't occur again?
Upvotes: 0
Views: 39
Reputation: 17907
The error message is a little confusing. It's quoting the line ('='*50)
, but the program actually crashes at
'Lines : '+lines+'\n'+
(I didn't count the lines, though.)
lines
is an integer. SO you'd need to wrap it with str(...)
. Same for the other integers you're printing.
Upvotes: 2