How delete unnecessary lines, replace column names and fix encoding in CSV file

Help is needed again.
Capture_04

Can you please share the file somewhere, else it is hard to help you.

sent the file in a personal message

Hm, it always seems to mess up the encoding. So I had to do the json to binary conversion and the parsing of the CSV file manually. But this should work now. Did add comments to explain what is happening:

{
  "nodes": [
    {
      "parameters": {
        "filePath": "./duhovoy_600-belyy_cutlist.csv"
      },
      "name": "Read Binary File",
      "type": "n8n-nodes-base.readBinaryFile",
      "typeVersion": 1,
      "position": [
        450,
        450
      ]
    },
    {
      "parameters": {
        "functionCode": "// Get binary data and convert to utf8 string\nconst data = Buffer.from(items[0].binary.data.data, 'base64').toString('utf8');\n \nconst newitems = [];\n\n// Split the different lines apart\nconst lines = data.split('\\r\\n');\n\n// Remove the first line which is here apparently not needed\nlines.shift();\n\n// Get the first line left and use the values as keys\nconst keys = lines.shift().split(';');\nlet values;\nlet itemData;\n\n// Go over the remaining lines\nfor (const line of lines) {\n  // Values seem to be separated by semicolon so split by it\n  values = line.split(';');\n  if (values.length !== keys.length) {\n    // All the ones which do not have the same amount of values as keys exist can be skipped\n    continue;\n  }\n  \n  // Create proper key -> values pairs\n  itemData = {};\n  for (let i=0;i<keys.length;i++) {\n    itemData[keys[i]] = values[i];\n  }\n  \n  // Add the new item\n  newitems.push({\n    json: itemData\n  }); \n}\n\nreturn newitems;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        450
      ]
    }
  ],
  "connections": {
    "Read Binary File": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Works great! Thank you, Jan!

Great to hear that it helped!