0

When I run this simple program in python I get an ascii error after "for line in file". I have tried many changes with no success. Why am I running into this issue?

Code:

flashcards = {}

def Gaelic():
    file = open ("gaelic_flashcard_mode.txt", "r")
    for line in file:
        print("clear4")
        line1 = line.rstrip().split("=")
        key = line1[0]
        trans = line1[1]
        PoS = line1[2]
        flashcards[key] = [trans, PoS]
    print(flashcards)

Text file being read in (gaelic_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)
3
  • Aside: it looks to me like flashcards will only wind up with the trans and PoS corresponding to the last key read for each of the keys. I.e. you'll only have 'her': ['a', '(adj)'], not ['ise', '(emphatic)'] or anything. Commented Sep 18, 2012 at 3:37
  • Works fine for me. I don't get any error. What python version are you using on what platform? Commented Sep 18, 2012 at 3:39
  • Did you copy-paste this code from somewhere else? Another text editor, perhaps? In that case, it's possible that the : is not an ascii :. Commented Sep 18, 2012 at 3:42

2 Answers 2

2

Are you using Python 3.X? Your print statements seem to indicate so.

Use the encoding parameter of open to specify the encoding of the source file.

Also, since you have multiple "keys" a dictionary can't hold the various versions of him, her, etc., so you probably want a list instead:

def Gaelic():
    with open('gaelic_flashcard_mode.txt','r',encoding='utf8') as f:
        return [tuple(line.rstrip().split('=')) for line in f]

print(Gaelic())

Output:

[('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)')]
Sign up to request clarification or add additional context in comments.

4 Comments

Your encoding method worked for me in Idle, but when I run it from terminal it says, "TypeError: 'encoding' is an invalid keyword argument for this function logout"
Were you running the same version of Python? The above requires Python 3.X. Python 2.X doesn't have an encoding parameter for built-in open.
Yes, I've been running Python 3.X for all of this.
My only guess is that ‘open‘ has been accidentally redefined, since ‘encoding‘ is a valid keyword for that function. Publish an standalone script demoing the problem with traceback.
1

Because you're using open() to open a non-ASCII text file. Use codecs.open() instead, passing it the appropriate encoding. And read this.

1 Comment

It looks like Python 3...in which case, open does work (it takes an optional encoding parameter)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.