base) PS F:\testpython> python .\test01.py Traceback (most recent call last): File…
- User
base) PS F:\testpython> python .\test01.py
Traceback (most recent call last): File "F:\testpython\test01.py", line 52, in package.write_to_file('pure-text-facts.apkg') File "D:\anaconda\Lib\site-packages\genanki\package.py", line 40, in write_to_file self.write_to_db(cursor, timestamp, id_gen) File "D:\anaconda\Lib\site-packages\genanki\package.py", line 60, in write_to_db deck.write_to_db(cursor, timestamp, id_gen) File "D:\anaconda\Lib\site-packages\genanki\deck.py", line 67, in write_to_db note.write_to_db(cursor, timestamp, self.deck_id, id_gen) File "D:\anaconda\Lib\site-packages\genanki\note.py", line 154, in write_to_db
self._check_invalid_html_tags_in_fields() File "D:\anaconda\Lib\site-packages\genanki\note.py", line 140, in _check_invalid_html_tags_in_fields invalid_tags = self._find_invalid_html_tags_in_field(field) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\anaconda\Lib\site-packages\genanki\note.py", line 136, in _find_invalid_html_tags_in_field return cls._INVALID_HTML_TAG_RE.findall(field) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: expected string or bytes-like object, got 'float' (base) PS F:\testpython> python .\test01.py Traceback (most recent call last): File "F:\testpython\test01.py", line 52, in package.write_to_file('pure-text-facts.apkg') File "D:\anaconda\Lib\site-packages\genanki\package.py", line 40, in write_to_file self.write_to_db(cursor, timestamp, id_gen) File "D:\anaconda\Lib\site-packages\genanki\package.py", line 60, in write_to_db deck.write_to_db(cursor, timestamp, id_gen) File "D:\anaconda\Lib\site-packages\genanki\deck.py", line 67, in write_to_db
note.write_to_db(cursor, timestamp, self.deck_id, id_gen) File "D:\anaconda\Lib\site-packages\genanki\note.py", line 154, in write_to_db
self._check_invalid_html_tags_in_fields() File "D:\anaconda\Lib\site-packages\genanki\note.py", line 140, in _check_invalid_html_tags_in_fields invalid_tags = self._find_invalid_html_tags_in_field(field) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\anaconda\Lib\site-packages\genanki\note.py", line 136, in _find_invalid_html_tags_in_field return cls._INVALID_HTML_TAG_RE.findall(field) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: expected string or bytes-like object, got 'float' (base) PS F:\testpython> - Assistant
The error you're encountering suggests that the
genankimodule, which is used for creating Anki decks programmatically, expects each field within a note to be a string or bytes-like object. However, it looks like one or more of the fields you're attempting to include in a note is a float (a floating-point number).In Python,
strandbytesare types that represent sequences of characters and bytes, respectively, whilefloatrepresents floating-point numbers, which are numeric values with decimal points, e.g., 3.14.The error occurs in the
_find_invalid_html_tags_in_fieldmethod withingenanki, when it tries to use a regular expression to find HTML tags in your note fields. This operation can only be performed on string or bytes-like objects, leading to theTypeErrorwhen it encounters a float.To fix this issue, you will need to ensure that all fields you pass to your notes are strings. If some of your data is numeric (floats or integers), you can convert it to a string using the
str()function in Python.Here is a simplified example of how you might adjust your code:
# Assuming 'my_float_field' is a float and 'my_other_field' is already a string my_float_field = 3.14 my_other_field = "Some text" # Convert float to string my_float_field_str = str(my_float_field) # Then pass these as fields note = genanki.Note( model=my_model, fields=[my_float_field_str, my_other_field] )You'll need to apply a similar adjustment to your code, ensuring that any numeric data is converted to strings before being passed as fields to a
genanki.Note. This should resolve theTypeErroryou're facing.