【问题标题】:'encoding' is an invalid keyword argument for this function [duplicate]“编码”是此函数的无效关键字参数[重复]
【发布时间】:2018-09-15 02:25:22
【问题描述】:
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','')+'|')

它的显示

TypeError Traceback(最近调用 最后)在() 1 导入编解码器 ----> 2 with open(filename+'.txt', 'a+', encoding='utf-8') as f: 3 用于 list_of_tweets 中的推文: 4 print(tweet.text.replace('\r','').replace('\n','')+'|') 5 f.write(tweet.text.replace('\r','').replace('\n','')+'|')

TypeError: 'encoding' 是该函数的无效关键字参数

【问题讨论】:

  • 你在用python2吗?
  • 你的 Python 版本是什么...?
  • 内置函数 open()python 2python 3 中具有不同的签名。

标签: python


【解决方案1】:

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

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','')+'|')

python2的常用open不接受编码。

【讨论】:

  • 谢谢,它帮助我从 python 3.x 迁移到 python 2.x
猜你喜欢
  • 1970-01-01
  • 2019-01-20
  • 2017-05-04
  • 2019-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-17
相关资源
最近更新 更多