2012-09-22 4 views
9

파이썬 프로그램에서 텍스트 파일을 여는 데 문제가 있습니다. 기본 열린 파일을 읽기 위해 사용할 때 ASCII 오류가 발생합니다. Idle에서 잘 작동하는 인코딩 매개 변수를 추가하여 누군가 나를 도왔지만 터미널을 통해 프로그램을 실행하면 다음 오류 메시지가 나타납니다. "TypeError : '인코딩'이이 함수의 잘못된 키워드 인수입니다. 어떻게 할 수 있습니까? 이 텍스트 파일을 읽어 데이터를 사용 하시겠습니까?TypeError : '인코딩'은이 함수에 대한 잘못된 키워드 인수입니다.

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

당신이에 실행해야하는 단말은 아마 표준으로 파이썬 2.x를 사용합니다.

명령 특히 단말기 "Python3"를 사용하여 시도 :

$ Python3 yourfile.py

3

+1 (시험 및 Python3이 잘 그것을 처리하는 2.7가 에러를 제공 확인하고). Linux와 관련하여 올바른 대답을 얻으려면 Unfun Cat에

그러나 Windows 사용자의 경우 'Python3'을 호출하면 일반적으로 작동하지 않습니다. 당신은 파이썬 3.3을 설치 한 경우 (당신이 다운로드 및 Windows 용 파이썬 실행기를 설치했을 경우), 당신은 입력 할 수 있습니다

C:\scr>py -3 yourfile.py 

사실이 실행도 너무에 다음 첫 번째 라인을 추가, 오두막 구문을 지원 스크립트의 파일은 상당히 (은/usr/빈은 Windows에서 무시됩니다) 크로스 플랫폼 작동합니다 : WINDOWS \ py.exe는 평 파일에 대한 기본 핸들러라고 가정하면, 그 일을 한 후

#! /usr/bin/python3 

을 수행 할 수 있습니다 그냥 유형 :

C:\scr>yourfile.py 

그리고 "평"는 귀하의 PATHEXT 환경 변수에있는 경우, 당신은 입력 할 수

C:\scr>yourfile 

상세 정보 :

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

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

관련 문제