Python ';编码';此函数的关键字参数无效

Python ';编码';此函数的关键字参数无效,python,Python,它的表演 TypeError回溯(最近的调用 最后)在() 1导入编解码器 ---->2打开时(文件名+'.txt',a+',编码为''utf-8')为f: 3对于推文列表中的推文: 4打印(tweet.text.replace('\r','').replace('\n','')+'|') 5 f.write(tweet.text.replace('\r','').replace('\n','')+'|') TypeError:“encoding”是此函数的无效关键字参数 如果您使用的是pyth

它的表演

TypeError回溯(最近的调用 最后)在() 1导入编解码器 ---->2打开时(文件名+'.txt',a+',编码为''utf-8')为f: 3对于推文列表中的推文: 4打印(tweet.text.replace('\r','').replace('\n','')+'|') 5 f.write(tweet.text.replace('\r','').replace('\n','')+'|')

TypeError:“encoding”是此函数的无效关键字参数


如果您使用的是python 2,请尝试:

import codecs
with open(filename+'.txt', 'a+', encoding='utf-8') as f:
    for tweet in list_of_tweets:
        print(tweet.text.replace('\r','').replace('\n','')+'|')
        f.write(tweet.text.replace('\r','').replace('\n','')+'|')

通常的OpenforPython2不接受编码。

你在使用python2吗?你的Python版本是什么…?内置函数
open()
在vs中有不同的签名。谢谢,它帮助我从Python 3.x迁移到Python 2.x
import codecs
from io import open
with open(filename+'.txt', 'a+', encoding='utf-8') as f:
    for tweet in list_of_tweets:
        print(tweet.text.replace('\r','').replace('\n','')+'|')
        f.write(tweet.text.replace('\r','').replace('\n','')+'|')