Python 脚本使用 genanki 创建 anki 抽认卡(预期的 str 实例,字典找到错误)



该程序使用熊猫获取在名为"List"的Excel文件的第一列中编写的英语单词列表,该列的标题是"单词",单词位于"Sheet1"中。

然后将单词存储为字符串列表。

PyDictionary和Googletrans通过创建词典和翻译器来利用,其中翻译器由翻译调用,以便将列表中的单词翻译成目标语言"丹麦语"。

然后创建一个简单的 for 循环,其中翻译单词列表中的每个翻译都打印其来源 ->其目的地和定义。

这在下面的代码中呈现:

from googletrans import Translator
import pandas as pd
from PyDictionary import PyDictionary
# Load excel file and parse the list of words as strings
file_location = "/Users/.../List.xlsx"
xl_workbook = pd.ExcelFile(file_location)
df = xl_workbook.parse("Sheet1")
aList = df['words'].tolist()
[str(i) for i in aList]
# Use PyDictionary to load definitions of words
dictionary = PyDictionary()
# Translate the list of strings into target language and give definitions
translator = Translator()
translations = translator.translate(aList, dest='da')
# Simple for-loop printing the words
for translation in translations:
print(
translation.origin, ' -> ', translation.text,
dictionary.meaning(translation.origin)
)

该程序实际运行并产生了我想要的结果。但是,在下一步中会出现此问题,如此处所述:

我希望将这些单词输入到我最喜欢的闪存卡程序Anki中。Anki是用Python编写的,有一个名为Genanki的非官方发行版。但是,这是我遇到问题的时候。

我现在在上面的代码中添加以下 4 件事:

  1. 我按照 genanki 的建议定义my_model,并创建了一个简单的抽认卡模型。这包括一个随机硬编码的数字(Anki需要(,模型的名称,一些字段和卡类型的模板。
  2. 我将my_deck定义为具有硬编码随机数和名称的特定抽认卡。
  3. 我将 for 循环更改为现在将翻译和定义直接运行到一个名为 aNote 的变量中,该变量由每次将注释添加到my_deck时遍历翻译的 genanki.note 运算符组成。
  4. 写了我的安基文件,我可以用Anki打开。

这可以在下面的代码中看到:

from googletrans import Translator
import pandas as pd
from PyDictionary import PyDictionary
import genanki
# Load excel file and parse the list of words as strings
file_location = "/Users/.../List.xlsx"
xl_workbook = pd.ExcelFile(file_location)
df = xl_workbook.parse("Sheet1")
aList = df['words'].tolist()
[str(i) for i in aList]
# Use PyDictionary to load definitions of words
dictionary = PyDictionary()
# Use genanki to define a flashcard model
my_model = genanki.Model(
2042686211,
'Simple Model',
fields=[
{'name': 'Question'},
{'name': 'Answer'},
],
templates=[
{
'name': 'Card 1',
'qfmt': '{{Question}}',
'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
},
])
# Specify the deck with genanki
my_deck = genanki.Deck(
1724897887,
'TestV3v1')
# Translate the list of strings with definition and add as note to anki
translator = Translator()
translations = translator.translate(aList, dest='da')
for translation in translations:
aNote = genanki.Note(
model=my_model, fields=[translation.origin, translation.text]
)
my_deck.add_note(aNote)
# Output anki file in desired folder
genanki.Package(my_deck).write_to_file(
'/Users/.../TestV3v1.apkg')

这段代码也可以很好地执行并生成一个可以在 Anki 中打开的文件,然后卡片在抽认卡的正面显示原始单词,背面显示翻译。

我的问题

为了完成我的项目,我希望在翻译随附的每张卡片的背面添加一个定义。我最初认为我只需要通过添加另一个字段来更正 my_model = genanki.model(...( 变量,这样我就可以将 dictionary.meaning(translation.origin( 添加到 for 循环中的笔记生成器中。

但是,当尝试仅添加定义以确保它们顺利运行时,我遇到了问题。请考虑以下代码:

for translation in translations:
aNote = genanki.Note(
model=my_model, fields=[translation.origin,
dictionary.meaning(translation.origin)
]
)
my_deck.add_note(aNote)

我希望卡片像往常一样打印,原始单词在正面,定义在背面,但是使用此 for 循环运行完整代码反而会给我以下错误:

Error: The Following Error occured: list index out of range
Error: The Following Error occured: list index out of range
Error: The Following Error occured: list index out of range
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: The Following Error occured: list index out of range
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: The Following Error occured: list index out of range
Error: The Following Error occured: list index out of range
Error: The Following Error occured: list index out of range
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: The Following Error occured: list index out of range
Error: A Term must be only a single word
Error: The Following Error occured: list index out of range
Traceback (most recent call last):
File "/Users/Lehmann/Desktop/XYZ/Programming/Translator/TranslatorProgramv3.py", line 51, in <module>
'/Users/Lehmann/Desktop/XYZ/Programming/Translator/TestV3v1.apkg')
File "/anaconda3/lib/python3.6/site-packages/genanki/__init__.py", line 313, in write_to_file
self.write_to_db(cursor, now_ts)
File "/anaconda3/lib/python3.6/site-packages/genanki/__init__.py", line 331, in write_to_db
deck.write_to_db(cursor, now_ts)
File "/anaconda3/lib/python3.6/site-packages/genanki/__init__.py", line 267, in write_to_db
note.write_to_db(cursor, now_ts, self.deck_id)
File "/anaconda3/lib/python3.6/site-packages/genanki/__init__.py", line 228, in write_to_db
self._format_fields(),        # flds
File "/anaconda3/lib/python3.6/site-packages/genanki/__init__.py", line 240, in _format_fields
return 'x1f'.join(self.fields)
TypeError: sequence item 1: expected str instance, dict found

我怀疑是"预期的str实例,字典找到"部分给我带来了麻烦,但是,这是我的第一个python项目,我不是程序员,所以我希望有人可以帮助我理解这个问题。

BR

米克尔

始终确保在使用变量时检查变量的类型。根据我的发现,使用 dictionary.meaning(...( 将类型更改为字典。所以你需要做的就是:

meaning=dictionary.meaning(translation.origin)
meaning_to_string=''.join('{}: {}'.format(key,val) for key,val in meaning.items())
#moving to aNote
aNote=genanki.Note(model=my_model, fields=[translation.origin,meaning_to_string])

相关内容

  • 没有找到相关文章

最新更新