> For the complete documentation index, see [llms.txt](https://doc.duaer.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.duaer.com/snippets/parse-csv-in-code-node.md).

# Parse CSV text in Duaer, including GBK files

In Duaer, read CSV files with Extract From CSV; parse CSV text from an API, or a GBK-encoded bank export, into one item per row with the Code node.
## Pick the right reader in Duaer

- A UTF-8 CSV file (form upload, email attachment, drive download): use Extract From CSV in [Extract from File](/build/extract-from-file.md). No code.
- An API that returns CSV: set Response Format to File under Options in [HTTP Request](/build/http-request.md), then Extract From CSV.
- A GBK-encoded export reads as garbage in Extract From CSV: use Extract From Text File in Extract from File, set File Encoding to gbk under Options, and parse the text in data with the code below.

## Parse CSV in the Duaer Code node

Add [Code](/build/code.md), keep Run Once for All Items, and paste:

```
function parseCsv(text, delimiter = ',') {
  const rows = [];
  let row = [];
  let field = '';
  let inQuotes = false;
  for (let i = 0; i < text.length; i++) {
    const c = text[i];
    if (inQuotes) {
      if (c === '"' && text[i + 1] === '"') { field += '"'; i++; }
      else if (c === '"') inQuotes = false;
      else field += c;
    } else if (c === '"') inQuotes = true;
    else if (c === delimiter) { row.push(field); field = ''; }
    else if (c === '\n' || c === '\r') {
      if (c === '\r' && text[i + 1] === '\n') i++;
      row.push(field); rows.push(row); row = []; field = '';
    } else field += c;
  }
  if (field !== '' || row.length) { row.push(field); rows.push(row); }
  return rows;
}

const out = [];
for (const item of $input.all()) {
  const [header, ...lines] = parseCsv(String(item.json.data).replace(/^\uFEFF/, ''));
  for (const cells of lines) {
    if (cells.every((v) => v === '')) continue;
    out.push({ json: Object.fromEntries(header.map((h, i) => [h.trim(), cells[i] ?? ''])) });
  }
}
return out;
```

It handles commas and line breaks inside quotes and doubled quotes, uses row one as headers, and skips empty rows. For semicolons pass ";" as the second argument to parseCsv; for tabs pass "\t".

## Duaer online tools

- [GBK to UTF-8 converter online (fix garbled CSV and TXT)](https://doc.duaer.com/tools/gbk-utf8-converter.md)
- [CSV to JSON converter (free, no upload)](https://doc.duaer.com/tools/csv-to-json.md)
## Questions

### Why does a CSV export show garbled text in Duaer?

The file is likely GBK. In Duaer, use Extract From Text File with File Encoding gbk, then parse it in the Code node.

### Can the Duaer Code node use a library like csv-parse?

By default the Duaer Code node cannot load third-party libraries; use the dependency-free parser on this page.

## Related

- [Read a file with Extract from File in Duaer](https://doc.duaer.com/build/extract-from-file.md)
- [Write a short step with Code in Duaer](https://doc.duaer.com/build/code.md)
- [Categorize bank statement lines and total them by category with Duaer](https://doc.duaer.com/tasks/categorize-bank-statements.md)
- [Merge several Excel files into one sheet with Duaer](https://doc.duaer.com/tasks/merge-excel-files.md)

