使用 write() 写入文本文件时出现 TypeError

TypeError when writing to text file with write()

我正在开发一个程序来计算德语文本中的英语程度,一切正常,除非尝试将结果输出到文本文件(在代码底部)。到目前为止,这是我的代码:

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()

这是我得到的错误:

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

我试过对变量使用 str() 但没有用。有谁知道如何正确使用 write(),这样这个错误就不会再发生了吗?

错误信息有点混乱。它引用了 ('='*50) 行,但程序实际上在

崩溃了
'Lines                 : '+lines+'\n'+

(不过我没数行数。)

lines 是一个整数。所以你需要用 str(...) 包装它。对于您正在打印的其他整数也是如此。