簡體   English   中英

類型錯誤:“編碼”是此函數的無效關鍵字參數

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

我的 Python 程序無法打開文本文件。 當我使用基本的打開文件進行讀取時,出現 ascii 錯誤。 有人幫我添加了一個在 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:

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)

您嘗試運行的終端可能使用 Python 2.x 作為標准。

嘗試在終端中專門使用命令“Python3”:

$ Python3 yourfile.py

(經過測試並確認 2.7 會出現該錯誤,並且 Python3 可以很好地處理它。)

使用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)

參考:見這個答案

+1 給 The Unfun Cat 以獲得關於 Linux 等的正確答案。

但是,對於 Windows 用戶,調用“Python3”通常是行不通的。 但是如果你已經安裝了 Python 3.3(或者你已經下載並安裝了 Python Launcher for Windows),你可以輸入:

C:\scr>py -3 yourfile.py

實際上,這個啟動器也支持 shebang 語法,因此將以下第一行添加到您的腳本文件中將可以跨平台工作(/usr/bin 在 Windows 上被忽略):

#! /usr/bin/python3

之后,假設 windows\\py.exe 是 .py 文件的默認處理程序,您只需鍵入:

C:\scr>yourfile.py

如果“.PY”在您的 PATHEXT 環境變量中,您只需鍵入:

C:\scr>yourfile

更多信息:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM