Pregunta

I have a spanish novel, in a plain textfile, and I want to make a Python script that puts a translation in brackets after difficult words. I have a list of the words (with translations) I want to do this with in a separate text file, which I have tried to format correctly. I've forgotten everything I knew about Python, which was very little to begin with, so I'm struggling. This is a script someone helped me with:

bookin = (open("C:\Users\King Kong\Documents\_div_tekstfiler_\coc_es.txt")).read() 
subin = open("C:\Users\King Kong\Documents\_div_tekstfiler_\cocdict.txt")
for line in subin.readlines():
    ogword, meaning = line.split()
    subword = ogword + " ("  + meaning + ")"
    bookin.replace(ogword, subword)
    ogword = ogword.capitalize()
    subword = ogword + " ("  + meaning + ")"
    bookin.replace(ogword, subword)
subin.close()
bookout = open("fileout.txt", "w")
bookout.write(bookin)
bookout.close()

When I ran this, I got this error message:

Traceback (most recent call last):
File "C:\Python27\translscript_secver.py", line 4, in <module>
ogword, meaning = line.split()
ValueError: too many values to unpack

The novel pretty big, and the dictionary I've made consists of about ten thousand key value pairs.

Does this mean there's something wrong with the dictionary? Or it's too big? Been researching this a lot, but I can't seem to make sense of it. Any advice would be appreciated.

¿Fue útil?

Solución

line.split() in ogword, meaning = line.split() returns a list, and in this case it may be returning more than 2 values. Write your code in a way that can handle more than two values. For instance, by assigning line.split() to a list and then asserting that the list has two items:

mylist = line.split()
assert len(mylist) == 2

Otros consejos

ogword, meaning = line.split()[:2]

line.split() return a list of words (space separated token) in line. The error you get suggest that somewhere, your dictionnary contains more than just pair. You may add trace message to locate the error (see below).

If your dictionnary contains richer definitions than synonym, you may use following lines, which put the first word in ogword and following ones in meaning.

words =  line.split()
ogword, meaning = words[0], " ".join(words[1:])

If your dictionary syntax is more complex (composed ogword), you have to rely on an explicit separator. You can still use split to divide your lines (line.split("=") will split a line on "=" characters)

Edit: to ignore and display bad lines, replace ogword, meaning = line.split() with

try:
    ogword,meaning = line.split()
except:
    print "wrong formated line:", line
    continue
split() 

returns a single list, ie one item, you are trying to assign this one thing to two variables.

It will work if the number of items in the list is equal to the number of variables on the left hand side of the assignment statement. I.e., the list is unpacked and the individual parts are assigned to the variables on the left hand side.

In this case, as pointed out by @Josvic Zammit, the problem can occur if there are more than 2 items in the list and can not properly "unpacked" and assigned.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top