Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类型错误:';编码';此函数的关键字参数无效_Python_Python 3.x - Fatal编程技术网

Python 类型错误:';编码';此函数的关键字参数无效

Python 类型错误:';编码';此函数的关键字参数无效,python,python-3.x,Python,Python 3.x,我的python程序无法打开文本文件。当我使用基本打开文件进行读取时,会出现ascii错误。有人帮了我的忙,让我添加了一个编码参数,这个参数在空闲状态下工作得很好,但是当我通过终端运行程序时,我得到了一条错误消息:“TypeError:“encoding”是这个函数的一个无效关键字参数”我如何读入这个文本文件来使用它的数据 try: import tkinter as tk from tkinter import * except: import Tkinter as t

我的python程序无法打开文本文件。当我使用基本打开文件进行读取时,会出现ascii错误。有人帮了我的忙,让我添加了一个编码参数,这个参数在空闲状态下工作得很好,但是当我通过终端运行程序时,我得到了一条错误消息:“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:

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)

您试图在其上运行此操作的终端可能使用Python2.x作为标准

尝试在终端中使用命令“Python3”:

$Python3 yourfile.py


(测试并确认2.7将给出该错误,Python3处理得很好。)

+1向Unfun Cat提供有关Linux等的正确答案

然而,对于Windows用户来说,调用“Python3”通常不起作用。但是,如果您已经安装了Python 3.3(或者已经下载并安装了Python Launcher for Windows),则可以键入:

C:\scr>py -3 yourfile.py
实际上,该启动器还支持shebang语法,因此将以下第一行添加到脚本文件中可以跨平台工作(在Windows上忽略/usr/bin):

完成此操作后,假设windows\py.exe是.py文件的默认处理程序,您只需键入:

C:\scr>yourfile.py
C:\scr>yourfile
如果PATHEXT环境变量中有“.PY”,则只需键入:

C:\scr>yourfile.py
C:\scr>yourfile
更多信息:

使用
io.open()
而不是
open
为我删除了此错误 例如:


如果您一直使用Python2.x,那么这就是您的方法