AttributeError: 'int' object has no attribute 'model'

AttributeError: 'int' object has no attribute 'model'

我正在尝试解决我发布的问题 here,即从一个 .csv 创建多个 1000 note .apkg 我是如何解释的,“我有一个脚本,它接收一个 .csv 文件并创建一个 apkg(anki 程序格式)。我想要的是为用户输入的每一定数量的行创建一个 .apkg 卡片组。例如,如果我有一个包含 4200 行的 deck.csv 文件,我选择将它分成 1000 个笔记组,它应该生成文件:

deck 1-1000.apkg
deck 1001-2000.apkg
deck 2001-3000.apkg
deck 3001-4000.apkg
deck 4001-4200.apkg

我现在试过的代码是。

import csv
import random
import genanki

# Filename of the data file
data_filename = str(input("Enter input file name with extension: "))

# Filename of the Anki deck to generate
deck_filename = data_filename.split('.')[0] + ".apkg"

# Title of the deck as shown in Anki
anki_deck_title = data_filename.split('.')[0]

# Name of the card model
anki_model_name = "Modelname"

# Create the deck model

model_id = random.randrange(1 << 30, 1 << 31)

style = """
.card {
 font-family: arial;
 font-size: 24px;
 text-align: center;
 color: black;
 background-color: white;
"""

anki_model = genanki.Model(
    model_id,
    anki_model_name,
    fields=[{"name": "front"}, {"name": "back"}],
    templates=[
        {
            "name": "Card 1",
            "qfmt": '{{front}}',
            "afmt": '{{FrontSide}}<hr id="answer">{{back}}</p>',
        },
    ],
    css=style,
)

# The list of flashcards
anki_notes = []

with open(data_filename, "r", encoding="utf-8") as csv_file:

    csv_reader = csv.reader(csv_file, delimiter=";")

    for row in csv_reader:
        anki_note = genanki.Note(
            model=anki_model,
            fields=[row[0], row[1]]
        )
        anki_notes.append(anki_note)

for subdeck in range(int((round((len(anki_notes)/1000)+0,5)))):
  anki_deck = genanki.Deck(model_id, anki_deck_title+" "+str((subdeck-1)*1000+1)+"-"+str(subdeck*1000))
  anki_package = genanki.Package(anki_deck)

  # Add flashcards to the deck
  for anki_note in range(1000):
      anki_deck.add_note(anki_note)

  # Save the deck to a file
  anki_package.write_to_file(deck_filename)
  print("Created deck with {} flashcards".format(len(anki_deck.notes)))

但是我得到了错误

Enter input file name with extension: CLDR.csv
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-23-a8b71afba302> in <module>()
     65 
     66   # Save the deck to a file
---> 67   anki_package.write_to_file(deck_filename)
     68   print("Created deck with {} flashcards".format(len(anki_deck.notes)))

2 frames
/usr/local/lib/python3.6/dist-packages/genanki/deck.py in write_to_db(self, cursor, timestamp, id_gen)
     59     models = json.loads(models_json_str)
     60     for note in self.notes:
---> 61       self.add_model(note.model)
     62     models.update(
     63       {model.model_id: model.to_json(timestamp, self.deck_id) for model in self.models.values()})

AttributeError: 'int' object has no attribute 'model'

我做错了什么?

我猜,但我认为你所有的问题都是因为你使用了两个名称相似的变量 anki_notesanki_note 或者因为你在两个地方使用了相同的名称 anki_note 和你可能会以错误的方式使用它。

首先你在创建笔记时使用anki_note

 anki_note = Note(...)

并且在 anki_note 中你有一个对象,它有字段 .model

但后来您使用 anki_note 表示整数。

 for anki_note in range(1000):

然后在下一行中将 anki_note 添加到 anki_deck 就像 Note

 #for anki_note in range(1000):
     anki_deck.add_note(anki_note)

所以最后你将整数添加到 anki_deck 而不是 Notes 并且后来错误显示在第 self.add_model(note.model) 行它得到整数而不是带有字段 .model[= 的对象35=]

所以你可能应该在 add_note

中使用 anki_notes[anki_note]
for anki_note in range(1000):
    anki_deck.add_note( anki_notes[anki_note] )

或者您应该使用 anki_notes 而不是 range(1000)

for anki_note in anki_notes:
    anki_deck.add_note( anki_note )

顺便说一句:

我没有测试它,但也许不是复杂的

for subdeck in range(int((round((len(anki_notes)/1000)+0,5)))):

str((subdeck-1)*1000+1) + "-" + str(subdeck*1000)

你需要更简单的

for subdeck in range(0, len(anki_notes), 1000):

str(subdeck+1) + "-" + str(subdeck+1000)