【问题标题】:Error: Encoding is an invalid keyword argument for this function错误:编码是此函数的无效关键字参数
【发布时间】:2019-06-05 19:00:30
【问题描述】:

我不断收到此错误:

TypeError: 'encoding' is an invalid keyword argument for this function.

当我运行我的代码时:

import re

f = open("/home/file/abc.sql", 'rt', encoding='latin-1')
source=f.read()

with open("/home/file/samp0l9.sql","w") as output:
    output.write(re.sub(r'(TBLPROPERTIES \(.*?\))', r'\1;', f, flags=re.DOTALL))

关于为什么会这样的任何想法?文件里面只是一些文本。

【问题讨论】:

  • 这段代码你运行的是什么版本的python?
  • 我(几乎)确定您运行的是 Python 2 而不是 Python 3。
  • 是的,你的权利。我什至没有注意到。
  • 这就解释了问题所在。内置的 open 函数在 Python 3 中仅接受 encoding

标签: python python-2.x


【解决方案1】:

Python 3

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

对比:

Python 2.7

open(name[, mode[, buffering]])

【讨论】:

    猜你喜欢
    • 2018-09-15
    • 2019-05-17
    • 2013-07-13
    • 2017-05-04
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多