LeadFlow / n8n CSV encoding
You exported a clean CSV from Excel, imported it into n8n, sent it to Google Sheets, and the sheet now says ?or?e Milo?evi?. The file is fine. The order of operations is not: n8n decoded the file before anyone checked what encoding it actually was.
| Where | What you see | What it means |
|---|---|---|
| n8n / Google Sheets | ?or?e Milo?evi? | Windows-1250 bytes decoded as UTF-8: every byte above 0x7F became a question mark |
| Google Sheets, opening the same file directly | Đorđe Milošević | Sheets sniffed the encoding and got it right - the file was never broken |
| Sheets after a second "fix" | Milošević | UTF-8 bytes decoded as Windows-1250: the mirror-image mistake, usually made while repairing the first one |
Both failures are the same bug in opposite directions - text decoded with the wrong code page. Once a byte has become a question mark, no parser option brings the letter back, which is why changing the CSV node's delimiter or quote settings never helps.
Excel's plain CSV (Comma delimited) export writes the system ANSI code page. On a Serbian, Croatian, Polish, Czech, Hungarian or Romanian Windows install that is Windows-1250, not UTF-8. n8n's Extract from File node decodes the file as UTF-8 by default, so 0xB9 (š) is not a valid UTF-8 start byte and is replaced.
There is a quieter second variant: Excel's Unicode Text export is UTF-16 with a BOM, and a node that assumes UTF-8 turns it into a file full of NUL characters.
Newer n8n does not leave you without a setting: since n8n 1.43 (2024, PR #9392) Extract from File has an Options → Encoding dropdown. Its default is still utf-8, and the values it offers are ASCII, Latin1, UCS-2, UCS2, UTF-8, UTF16LE and UTF8. Windows-1250 is not among them, and nothing in the node detects the encoding for you - so for a Serbian, Croatian, Czech or Hungarian Excel export the correct value simply is not in the list, and Latin1 cannot stand in for it: ISO-8859-1 has no đ, Đ, ć or Ć at all.
Put a Code node named Decode CSV bytes between the node that reads the file and the node that parses it. It looks at the bytes instead of trusting a default - BOM first, then strict UTF-8, then Windows-1250 - rewrites the file as UTF-8, and records which encoding it found in lfEncoding, so the choice is visible downstream instead of silent.
data - the output of Read/Write Files from Disk or a Google Drive Download.// wf01-decode-node.js - source for the "Decode CSV bytes" Code node of the PAID pack's
// workflow 01 (CSV -> Google Sheets). Not executed directly: sales/make-wf01-encoding.mjs
// inlines this file into workflows/01-lead-import-csv.json, replacing \u20ac\u0081\u201a\u0083\u201e\u2026\u2020\u2021\u0088\u2030\u0160\u2039\u015a\u0164\u017d\u0179\u0090\u2018\u2019\u201c\u201d\u2022\u2013\u2014\u0098\u2122\u0161\u203a\u015b\u0165\u017e\u017a\u00a0\u02c7\u02d8\u0141\u00a4\u0104\u00a6\u00a7\u00a8\u00a9\u015e\u00ab\u00ac\u00ad\u00ae\u017b\u00b0\u00b1\u02db\u0142\u00b4\u00b5\u00b6\u00b7\u00b8\u0105\u015f\u00bb\u013d\u02dd\u013e\u017c\u0154\u00c1\u00c2\u0102\u00c4\u0139\u0106\u00c7\u010c\u00c9\u0118\u00cb\u011a\u00cd\u00ce\u010e\u0110\u0143\u0147\u00d3\u00d4\u0150\u00d6\u00d7\u0158\u016e\u00da\u0170\u00dc\u00dd\u0162\u00df\u0155\u00e1\u00e2\u0103\u00e4\u013a\u0107\u00e7\u010d\u00e9\u0119\u00eb\u011b\u00ed\u00ee\u010f\u0111\u0144\u0148\u00f3\u00f4\u0151\u00f6\u00f7\u0159\u016f\u00fa\u0171\u00fc\u00fd\u0163\u02d9 with
// the Windows-1250 upper half escaped as U+ hex codes, so no encoding pass can corrupt it.
//
// Plain ES5-ish JS on purpose: it runs inside n8n's Code node sandbox, where Buffer,
// TextDecoder and atob are not guaranteed to exist.
//
// The problem it fixes: n8n's "Extract from File" reads a CSV as UTF-8. Excel on a Serbian,
// Polish, Czech or Hungarian Windows writes Windows-1250 by default, so a name with
// D-stroke, c-caron or s-caron arrives as question marks and the buyer's Google Sheet
// stores the wreckage. This node looks at the bytes (BOM, then strict UTF-8, then
// Windows-1250), rewrites the file as UTF-8 and records which encoding it used, so the
// CSV parser downstream only ever sees valid UTF-8.
const items = $input.all();
if (!items.length) return [];
// Windows-1250, bytes 0x80-0xFF.
const CP1250 = "\u20ac\u0081\u201a\u0083\u201e\u2026\u2020\u2021\u0088\u2030\u0160\u2039\u015a\u0164\u017d\u0179\u0090\u2018\u2019\u201c\u201d\u2022\u2013\u2014\u0098\u2122\u0161\u203a\u015b\u0165\u017e\u017a\u00a0\u02c7\u02d8\u0141\u00a4\u0104\u00a6\u00a7\u00a8\u00a9\u015e\u00ab\u00ac\u00ad\u00ae\u017b\u00b0\u00b1\u02db\u0142\u00b4\u00b5\u00b6\u00b7\u00b8\u0105\u015f\u00bb\u013d\u02dd\u013e\u017c\u0154\u00c1\u00c2\u0102\u00c4\u0139\u0106\u00c7\u010c\u00c9\u0118\u00cb\u011a\u00cd\u00ce\u010e\u0110\u0143\u0147\u00d3\u00d4\u0150\u00d6\u00d7\u0158\u016e\u00da\u0170\u00dc\u00dd\u0162\u00df\u0155\u00e1\u00e2\u0103\u00e4\u013a\u0107\u00e7\u010d\u00e9\u0119\u00eb\u011b\u00ed\u00ee\u010f\u0111\u0144\u0148\u00f3\u00f4\u0151\u00f6\u00f7\u0159\u016f\u00fa\u0171\u00fc\u00fd\u0163\u02d9";
const B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
function b64ToBytes(s) {
const clean = String(s).replace(/^data:[^,]*,/, "").replace(/[^A-Za-z0-9+/=]/g, "");
const out = [];
let buf = 0;
let bits = 0;
for (let i = 0; i < clean.length; i++) {
const c = clean.charCodeAt(i);
if (c === 61) break;
let v = -1;
if (c >= 65 && c <= 90) v = c - 65;
else if (c >= 97 && c <= 122) v = c - 71;
else if (c >= 48 && c <= 57) v = c + 4;
else if (c === 43) v = 62;
else if (c === 47) v = 63;
if (v < 0) continue;
buf = (buf << 6) | v;
bits += 6;
if (bits >= 8) {
bits -= 8;
out.push((buf >> bits) & 0xFF);
}
}
return out;
}
function bytesToB64(b) {
let out = "";
for (let i = 0; i < b.length; i += 3) {
const b1 = b[i];
const b2 = i + 1 < b.length ? b[i + 1] : undefined;
const b3 = i + 2 < b.length ? b[i + 2] : undefined;
out += B64[b1 >> 2];
out += B64[((b1 & 3) << 4) | ((b2 === undefined ? 0 : b2) >> 4)];
out += b2 === undefined ? "=" : B64[((b2 & 15) << 2) | ((b3 === undefined ? 0 : b3) >> 6)];
out += b3 === undefined ? "=" : B64[b3 & 63];
}
return out;
}
// Strict UTF-8. Returns null on the first sequence that is not valid UTF-8: lenient
// decoding would turn bad bytes into replacement characters and we would never learn
// that the file was not UTF-8 at all.
function utf8Strict(b, start) {
let out = "";
let i = start || 0;
while (i < b.length) {
const c = b[i++];
if (c < 0x80) { out += String.fromCharCode(c); continue; }
let need;
let cp;
if (c >= 0xC2 && c <= 0xDF) { need = 1; cp = c & 0x1F; }
else if (c >= 0xE0 && c <= 0xEF) { need = 2; cp = c & 0x0F; }
else if (c >= 0xF0 && c <= 0xF4) { need = 3; cp = c & 0x07; }
else return null;
if (i + need > b.length) return null;
for (let k = 0; k < need; k++) {
const cc = b[i++];
if ((cc & 0xC0) !== 0x80) return null;
cp = (cp << 6) | (cc & 0x3F);
}
if (cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) return null;
if (cp > 0xFFFF) {
const v = cp - 0x10000;
out += String.fromCharCode(0xD800 + (v >> 10), 0xDC00 + (v & 0x3FF));
} else {
out += String.fromCharCode(cp);
}
}
return out;
}
function utf8Encode(str) {
const out = [];
for (let i = 0; i < str.length; i++) {
let cp = str.charCodeAt(i);
if (cp >= 0xD800 && cp <= 0xDBFF && i + 1 < str.length) {
const lo = str.charCodeAt(i + 1);
if (lo >= 0xDC00 && lo <= 0xDFFF) {
cp = 0x10000 + ((cp - 0xD800) << 10) + (lo - 0xDC00);
i++;
}
}
if (cp < 0x80) out.push(cp);
else if (cp < 0x800) out.push(0xC0 | (cp >> 6), 0x80 | (cp & 0x3F));
else if (cp < 0x10000) out.push(0xE0 | (cp >> 12), 0x80 | ((cp >> 6) & 0x3F), 0x80 | (cp & 0x3F));
else out.push(0xF0 | (cp >> 18), 0x80 | ((cp >> 12) & 0x3F), 0x80 | ((cp >> 6) & 0x3F), 0x80 | (cp & 0x3F));
}
return out;
}
function cp1250Decode(b) {
let out = "";
for (let i = 0; i < b.length; i++) {
const c = b[i];
out += c < 0x80 ? String.fromCharCode(c) : CP1250.charAt(c - 0x80);
}
return out;
}
function utf16Decode(b, start, little) {
let out = "";
for (let i = start; i + 1 < b.length; i += 2) {
const u = little ? b[i] | (b[i + 1] << 8) : (b[i] << 8) | b[i + 1];
out += String.fromCharCode(u);
}
return out;
}
function decodeBytes(b) {
if (b.length >= 3 && b[0] === 0xEF && b[1] === 0xBB && b[2] === 0xBF) {
const s = utf8Strict(b, 3);
if (s !== null) return { text: s, encoding: "UTF-8 (BOM)" };
}
if (b.length >= 2 && b[0] === 0xFF && b[1] === 0xFE) return { text: utf16Decode(b, 2, true), encoding: "UTF-16 LE (BOM)" };
if (b.length >= 2 && b[0] === 0xFE && b[1] === 0xFF) return { text: utf16Decode(b, 2, false), encoding: "UTF-16 BE (BOM)" };
const u = utf8Strict(b, 0);
if (u !== null) return { text: u, encoding: "UTF-8" };
return { text: cp1250Decode(b), encoding: "Windows-1250" };
}
// --- a banner row above the header -------------------------------------------
// Exports often start with a line that is not the header: a blank line, Excel's
// "sep=," directive, or a title such as "Export leadova 12.11.2026". The CSV
// parser downstream treats the FIRST line as the header, so one banner line makes
// every column unrecognised and the run stops with "no address column" on a file
// that is perfectly fine. The whole text is available here, before parsing, so
// leading lines that cannot be a header are dropped - and a one-field line is only
// dropped when a later line in the same file really does look like a multi-column
// header, so a single-column file is never touched. What was dropped is reported
// on the item as lfSkippedBanner.
// Quote-aware line split: a newline inside a quoted cell does not end the line.
function splitLines(text) {
const lines = [];
let cur = "";
let inQ = false;
for (let i = 0; i < text.length; i++) {
const ch = text.charAt(i);
if (ch === '"') {
if (inQ && text.charAt(i + 1) === '"') { cur += '""'; i++; continue; }
inQ = !inQ;
cur += ch;
continue;
}
if ((ch === "\n" || ch === "\r") && !inQ) {
if (ch === "\r" && text.charAt(i + 1) === "\n") i++;
lines.push(cur);
cur = "";
continue;
}
cur += ch;
}
lines.push(cur);
return lines;
}
function countFields(line, delim) {
let n = 1;
let inQ = false;
for (let i = 0; i < line.length; i++) {
const ch = line.charAt(i);
if (ch === '"') {
if (inQ && line.charAt(i + 1) === '"') { i++; continue; }
inQ = !inQ;
continue;
}
if (!inQ && ch === delim) n++;
}
return n;
}
function detectDelimiter(lines) {
const score = {};
let best = ",";
let bestCount = 0;
for (let d = 0; d < 4; d++) score[d] = 0;
const candidates = [",", ";", "\t", "|"];
let seen = 0;
for (let i = 0; i < lines.length && seen < 6; i++) {
if (!/\S/.test(lines[i])) continue;
seen++;
for (let d = 0; d < candidates.length; d++) {
score[d] += countFields(lines[i], candidates[d]) - 1;
}
}
for (let d = 0; d < candidates.length; d++) {
if (score[d] > bestCount) { bestCount = score[d]; best = candidates[d]; }
}
return best;
}
function stripBanner(text) {
const lines = splitLines(text);
const delim = detectDelimiter(lines);
const dropped = [];
let at = 0;
while (at < lines.length && at < 3) {
const line = lines[at];
if (!/\S/.test(line)) { dropped.push("(blank line)"); at = at + 1; continue; }
// Excel's own directive ("sep=," / "sep=;") is one line, not a header - and it
// contains a delimiter, so field counting alone would mistake it for one.
if (/^sep=.$/i.test(line.replace(/^[ \t]+|[ \t]+$/g, ""))) { dropped.push(line.slice(0, 60)); at = at + 1; continue; }
if (countFields(line, delim) > 1) break;
let headerAfter = 0;
for (let k = at + 1; k < lines.length && k < at + 5; k++) {
if (countFields(lines[k], delim) > 1) { headerAfter = k; break; }
}
if (!headerAfter) break;
dropped.push(line.slice(0, 60));
at = at + 1;
}
if (!at) return { text: text, dropped: [] };
return { text: lines.slice(at).join("\n"), dropped: dropped };
}
return items.map(function (item) {
const bin = item.binary && item.binary.data;
if (!bin || typeof bin.data !== "string") {
throw new Error("Decode CSV bytes: no file on the incoming item. Connect a Read/Write File node first, so the file arrives as the binary property data.");
}
const decoded = decodeBytes(b64ToBytes(bin.data));
const cleaned = stripBanner(decoded.text);
const utf8 = utf8Encode(cleaned.text);
const json = {};
Object.keys(item.json || {}).forEach(function (k) { json[k] = item.json[k]; });
json.lfEncoding = decoded.encoding;
if (cleaned.dropped.length) json.lfSkippedBanner = cleaned.dropped.join(" | ");
const binary = {};
Object.keys(item.binary || {}).forEach(function (k) { binary[k] = item.binary[k]; });
binary.data = {};
Object.keys(bin).forEach(function (k) { binary.data[k] = bin[k]; });
binary.data.data = bytesToB64(utf8);
binary.data.mimeType = "text/csv";
binary.data.fileSize = utf8.length;
return { json: json, binary: binary };
});
This is the exact node shipped in workflow 01 of the LeadFlow pack, not a sketch of one. It is plain ES5-style JavaScript on purpose: n8n's Code sandbox does not guarantee Buffer, TextDecoder or atob, so the base64 and UTF-8 codecs are inline. No network call, no dependency, no credential.
lfEncoding: UTF-8, UTF-8 (BOM), UTF-16 LE (BOM) or Windows-1250.One file, one repair, no install: the free browser CSV repair decodes the real encoding, dedupes, and gives back a CSV that Excel reopens correctly (it writes a BOM for exactly that reason). It runs entirely in your tab - the file is never uploaded, because there is no server to upload it to.
The decode step is one node of workflow 01 (CSV to Google Sheets) in the LeadFlow Automation Pack: import, dedupe by email, normalise headers (including Ime, Prezime, E-mail adresa), then write to the sheet. The pack is four workflows, a zero-setup demo that runs with no credential, the setup guide, the delivery script and the troubleshooting page.
Get the pack - $49 launch Run the demo first
$49 launch price (standard $79), one-time, commercial licence, 14-day refund on request. The ZIP is on the receipt page the moment you pay - one click, no waiting for an email. Not sure which option fits your file? Write to perun.intelligence@gmail.com before you pay - and if the button will not open, that address is the fallback.
What the receipt says: the charge comes from our billing account dragana.karans@gmail.com — that is this site, not a stranger. Files, questions and the refund go to perun.intelligence@gmail.com.
Why does n8n show question marks when Google Sheets shows the names correctly?
Google Sheets sniffs the bytes and n8n does not. Excel on a Serbian, Croatian, Polish, Czech or Hungarian Windows writes CSV in Windows-1250, while the n8n Extract from File node decodes the file as UTF-8 unless its Encoding option is changed - and that option offers no Windows-1250. Every byte above 0x7F that is not valid UTF-8 becomes a question mark, and that is what reaches the sheet.
Is the CSV node itself at fault?
No. The parser does its job on the text it was handed; the file was decoded before it got there. A decode step between the file node and the parser is the fix. Changing delimiter or quote options will not bring the letters back, because the bytes are already gone.
Do I have to use a Code node?
First check Options -> Encoding on the node (n8n 1.43 and later): it lists ASCII, Latin1, UCS-2, UCS2, UTF-8 and UTF16LE, no Windows-1250, and Latin1 is ISO-8859-1, which has no đ, Đ, ć or Ć. If your own system writes the file, save it as CSV UTF-8 (in Excel that is the CSV UTF-8 (Comma delimited) option) and skip the node. If the file arrives from somebody else Excel, their export is Windows-1250 and you cannot control it - the node is the reliable option.
Does it handle Cyrillic, Greek or Japanese CSV?
No, and it will not pretend to. Detection is BOM, then strict UTF-8, then Windows-1250. A Windows-1251 Cyrillic export is decoded as 1250 and the letters come out wrong - use a Cyrillic-aware conversion, or ask the sender for a UTF-8 export.
Does the node send my file anywhere?
No. It is plain JavaScript inside your own n8n Code node: it reads the binary property, rewrites it as UTF-8 and returns it. No network call, no API key, no dependency.