2012-09-22 21 views
9

私のpythonプログラムでは、テキストファイルを開く際に問題があります。基本オープンファイルを読み込みに使用すると、ASCIIエラーが発生します。 Idleでうまく動作するエンコーディングパラメータを追加することで誰かが助けてくれましたが、ターミナル経由でプログラムを実行すると、次のエラーメッセージが表示されます。 "TypeError: 'エンコーディングはこの関数の無効なキーワード引数です。このテキストファイルを読み込んでデータを使用しますか?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) 

答えて

12

ターミナルは、おそらく標準としてPython 2.xのを使用しています。

コマンド特にターミナルで "のpython3" を使用してみてください:

$ Python3 yourfile.py

3

+1(テスト済みとのpython3はうまくそれを処理する2.7は、そのエラーを与えることが確認できます。) The Unfun CatにLinuxに関する正しい答えがあります。

しかし、Windowsユーザーの場合、 'Python3'を呼び出すのは一般的には機能しません。しかし、あなたは、Python 3.3をインストールしている場合(または、あなたがダウンロードし、Windows用のPythonランチャーをインストールした場合)、次のように入力することができます

C:\scr>py -3 yourfile.py 

を実際に、このランチャーはまたそうに次の最初の行を追加し、シェバング構文をサポートしていますスクリプトのファイルは、ディレクトリ(/ usr/binには、Windows上で無視される)かなりクロスプラットフォームを動作します:

#! /usr/bin/python3 

ことをやった後、py.exe \ Windowsはあなたができる、の.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/

関連する問題