Import bug (2.1.54)

For years, I’ve been importing into Anki from text files separated by semi-colons.

Today, the import function skipped about fifty cards from this one:

01282;"I'm happy because soon I'll get rid of you and I'll be able to go back to eating meat», my friend whose brother is a musician told me a week before I left the country to do the follow-up exams.;«Sono contenta perché presto mi libererò di te e potrò tornare a mangiare carne», mi disse la mia amica dal fratello musicista una settimana prima che lasciassi il paese per fare gli esami di controllo.<br><br><i>dei musicisti</i>;

You may note that I had a mismatch of quotation marks in field two: it opens with " and closes with ».

As a result, for note 01282, Anki imported all notes up to 01331’s field 3 into field 3 (01331 also has a ":

01331;At a time when social rights are dwindling for all and the supreme leader sharpens people's fears, "Become Outraged" is a warning to live constructively.<br><br>(two options for <i>warning</i>);In un'epoca nella quale i diritti sociali si assottigliano per tutti e il capo supremo acuisce le paure della gente, «Indignatevi» è un monito per vivere costruttivamente.<br><br>/<i>un avvertimento, un avviso, un'avvertenza</i><br><i>assottigliare</i>: to thin | to sharpen, along with <i>affilare</i><br><i>acuire</i>: HOE → (i) <i>Rendere acuto e penetrante, spec. fig.: acuire. la vista, l'ingegno, il desiderio, la passione SIN. aguzzare,</i> (ii) <i>acuìrsi. Farsi più acuto: si acuiva in lui l'ansia dell'attesa SIN. acutizzarsi</i><br>/<i>accentuare, esacerbare, aggravare, peggiorare</i><br><i>un monito</i>: HOE → <i>Ammonimento severo, avvertimento solenne: che queste mie parole ti siano di m.</i>;

Why should Anki care whether quotes within notes are well-formed or not? To me that makes no sense, there are situations where one may legitimately use only one of a pair of quotes. Only separators should matter. But maybe I’m missing something.

1 Like

" is used to escape special characters like field and line separators in CSV. If you misplace quotation marks, it’s unclear where fields and lines start or end and different CSV libraries will interpret the invalid file inconsistently.

1 Like

You’re right, but

  1. To me it sounds like a runaway regex, jumping over the natural boundaries of the field separators,
  2. It’s unexpected, not a documented feature. The manual doesn’t say “watch out, if you use a single double quote in a field the import function will break”.

From the standpoint of user experience, it’s a bug.

When you include quotes inside CSV files, you’re telling the CSV parser to ignore the “natural boundaries” and include the field separators as part of the field until the quotes are closed. If you try import the following small sample into either Excel, LibreOffice, or even old versions of Anki, you’ll see that you end up with only two rows there as well, instead of the four that you seem to be expecting.

one,"two
three,four
five,"six"
"seven","eight"

Your complaint seems to be that Anki should have done what you intended, instead of what you told it to do. I’m afraid that’s a rather hard problem.

2 Likes

Hi, Damien. If you’re telling me that it’s intentional, then no problem. I just never noticed it in the manual. Thank you for clarifying.

Escape the multi-lines by placing the contents of the field in quotation marks:

hello; "this is
a two line answer"
two; this is a one line one

Because quotes are used to mark where a field begins and ends, if you wish to include them inside your field, you need to replace a single doublequote with two doublequotes to “escape” them from the regular handling, like so:

field one;"field two with ""escaped quotes"" inside it"

When you use a spreadsheet program like Libreoffice to create the CSV file for you, it will automatically take care of escaping double quotes.

https://docs.ankiweb.net/importing.html#text-files

1 Like

Thank you very much, Rumo! Wishing you all a fun weekend.

2 Likes

Hi Rumo, in your understanding, how should I escape the following quote within field two so it displays correctly?

01282;"I'm happy because soon I'll get rid of you and I'll be able to go back to eating meat", my friend whose brother is a musician told me a week before I left the country to do the follow-up tests.;

I tried this:

01282;""I'm happy because soon I'll get rid of you and I'll be able to go back to eating meat"", my friend whose brother is a musician told me a week before I left the country to do the follow-up tests.;

The first quote does not appear when the note is displayed in Anki. The second quote appears doubled.

Of course it might be easier to switch to curlies. :slight_smile:

There are two steps to include literal double quotes:

  1. Wrap the field in double quotes.
  2. Double all double quotes inside that field.

In your example:

01282;"""I'm happy because soon I'll get rid of you and I'll be able to go back to eating meat"", my friend whose brother is a musician told me a week before I left the country to do the follow-up tests.";

Spreadsheet programs will do this for you.

1 Like

Fantastic. Thank you, Rumo!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.