English 中文(简体)
• 设法使用“灰色”文字,添加str,以备
原标题:Trying to use Python script to add strings to file

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:UsersKing KongDocuments\_div_tekstfiler_coc_es.txt")).read() 
subin = open("C:UsersKing KongDocuments\_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()

当我这样做时,我发出这一错误信息:

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

新颖的粗金字大,一根字典由大约1 000个关键价值奶粉组成。

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.

最佳回答

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
问题回答
ogword, meaning = line.split()[:2]

<编码>line.split(>> 回到一行的字句(与点分开的空间)。 你发现的错误表明,在有些地方,你的独裁者不止是air。 您可添加追踪信息,以找出错误(见下文)。

如果你的字典含有比同义词更丰富的定义,你可使用以下措辞:第一句话用词句和后面的词句。

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: 忽视和显示坏线,取而代之的是ogword, 意思=行文()。

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

一份单一清单,即一个项目,你试图把这一件事分配给两个变量。

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.

在本案中,正如“Josvic Zammit”所指出的,如果名单上有2个以上物品,而且不能适当“未包装”和分配,就会出现问题。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签