简体   繁体   English

类型错误:“编码”是此函数的无效关键字参数

[英]TypeError: 'encoding' is an invalid keyword argument for this function

My python program has trouble opening a text file.我的 Python 程序无法打开文本文件。 When I use the basic open file for read, I get an ascii error.当我使用基本的打开文件进行读取时,出现 ascii 错误。 Someone helped me out by having me add an encoding parameter that works well in Idle, but when I run the program through terminal, I get this error message: "TypeError: 'encoding' is an invalid keyword argument for this function" How can I read this text file in to use it's data?有人帮我添加了一个在 Idle 中运行良好的编码参数,但是当我通过终端运行程序时,我收到此错误消息:“TypeError:'encoding' 是此函数的无效关键字参数”我怎么办读入这个文本文件以使用它的数据?

try:
    import tkinter as tk
    from tkinter import *
except:
    import Tkinter as tk
    from Tkinter import *

import time
import sys
import os
import random

flashcards = {}


def Flashcards(key, trans, PoS):
    if not key in flashcards:
        flashcards[key] = [[trans], [PoS]]
    else:
        x = []
        for item in flashcards[key][0]:
            x.append(item)
        x.append(trans)
        flashcards[key][0] = x
        x = []
        for item in flashcards[key][1]:
            x.append(item)
        x.append(PoS)
        flashcards[key][1] = x


def ImportGaeilge():
    flashcards = {}
    with open('gaeilge_flashcard_mode.txt','r', encoding='utf8') as file:
        for line in file:
            line1 = line.rstrip().split("=")
            key = line1[0]
            trans = line1[1]
            PoS = line1[2]
            Flashcards(key, trans, PoS)

def Gaeilge():
    numberCorrect = 0
    totalCards = 0
    ImportGaeilge()
    wrongCards = {}
    x = input('Hit "ENTER" to begin. (Type "quit" to quit)')
    while x != quit:
        os.system('cls')
        time.sleep(1.3)
        card = flashcards.popitem()
        if card == "":
## WRONG CARDS
            print ("Deck one complete.")
            Gaeilge()
        print("\n\n")
        print(str(card[0])+":")
        x = input("\t:")
        if x == 'quit':
            break
        else:
            right = False
            for item in card[1]:
                if x == card[1]:
                    right = True
                    print("\nCorrect!")
                    numberCorrect += 1
            if right == False:
                print(card[0])

        totalCards += 1
        print("Correct answers:", str(numberCorrect) +"/"+str(totalCards))


Gaeilge()

gaeilge_flashcard_mode.txt: gaeilge_flashcard_mode.txt:

I=mé=(pron) (emphatic)
I=mise=(n/a)
you=tú=(pron) (subject)
you=tusa=(emphatic)
y'all=sibh=(plural)
y'all=sibhse=(emphatic)
he=sé=(pron)
he=é=(n/a)
he=seisean=(emphatic)
he=eisean=(n/a)
she=sí=(pron)
she=í=(n/a)
she=sise=(emphatic)
she=ise=(emphatic)
him=é=(pron)
him=eisean=(emphatic)
her=í=(pron)
her=ise=(emphatic)
her=a=(adj)

The terminal you are trying to run this on probably uses Python 2.x as standard.您尝试运行的终端可能使用 Python 2.x 作为标准。

Try using the command "Python3" specifically in the terminal:尝试在终端中专门使用命令“Python3”:

$ Python3 yourfile.py

(Tested and confirmed that 2.7 will give that error and that Python3 handles it just fine.) (经过测试并确认 2.7 会出现该错误,并且 Python3 可以很好地处理它。)

using io.open() instead of open removed this error for me eg:使用io.open()而不是open为我删除了这个错误,例如:

import io
with io.open('gaeilge_flashcard_mode.txt','r', encoding='utf8') as file:
    for line in file:
        line1 = line.rstrip().split("=")
        key = line1[0]
        trans = line1[1]
        PoS = line1[2]
        Flashcards(key, trans, PoS)

reference: see this answer参考:见这个答案

+1 to The Unfun Cat for a correct answer regarding Linux etc. +1 给 The Unfun Cat 以获得关于 Linux 等的正确答案。

For Windows users, however, calling 'Python3' generally won't work.但是,对于 Windows 用户,调用“Python3”通常是行不通的。 But if you've installed Python 3.3 (or if you've downloaded and installed Python Launcher for Windows), you can type:但是如果你已经安装了 Python 3.3(或者你已经下载并安装了 Python Launcher for Windows),你可以输入:

C:\scr>py -3 yourfile.py

Actually, this launcher also supports shebang syntax, so adding the following first line to your script's file will work fairly cross-platform (the /usr/bin is ignored on Windows):实际上,这个启动器也支持 shebang 语法,因此将以下第一行添加到您的脚本文件中将可以跨平台工作(/usr/bin 在 Windows 上被忽略):

#! /usr/bin/python3

After doing that, assuming that windows\\py.exe is the default handler for .py files, you can just type:之后,假设 windows\\py.exe 是 .py 文件的默认处理程序,您只需键入:

C:\scr>yourfile.py

And if ".PY" is in your PATHEXT environment variable, you can just type:如果“.PY”在您的 PATHEXT 环境变量中,您只需键入:

C:\scr>yourfile

More info:更多信息:

http://docs.python.org/3/whatsnew/3.3.html http://docs.python.org/3/whatsnew/3.3.html

http://www.python.org/dev/peps/pep-0397/ http://www.python.org/dev/peps/pep-0397/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:编码是此函数的无效关键字参数 - Error: Encoding is an invalid keyword argument for this function Django TypeError(“'%s'是此函数的无效关键字参数”) - Django TypeError(“'%s' is an invalid keyword argument for this function”) Django TypeError:'bar'是此函数的无效关键字参数 - Django TypeError: 'bar' is an invalid keyword argument for this function opencv TypeError: 'interpolation' is an invalid keyword argument for this function - opencv TypeError: 'interpolation' is an invalid keyword argument for this function 类型错误:“数据”是此函数的无效关键字参数 - TypeError: 'data' is an invalid keyword argument for this function Django-TypeError:“类型”是此函数的无效关键字参数 - Django - TypeError: 'genre' is an invalid keyword argument for this function TypeError:“ key”是此函数的无效关键字参数 - TypeError: 'key' is an invalid keyword argument for this function Django:TypeError:“ x”是此函数的无效关键字参数 - Django: TypeError: 'x' is an invalid keyword argument for this function TypeError:'cmp'是此函数的无效关键字参数 - TypeError: 'cmp' is an invalid keyword argument for this function TypeError:“ person”是此函数的无效关键字参数 - TypeError: 'person' is an invalid keyword argument for this function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM