# Msg.payload ---\> Function Python

**URL:** https://discourse.nodered.org/t/msg-payload-function-python/96339
**Category:** General
**Tags:** function-node
**Created:** [30 March 2025 10:32 UTC](https://discourse.nodered.org/t/msg-payload-function-python/96339 "2025-03-30T10:32:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![keyooo2](https://avatars.discourse-cdn.com/v4/letter/k/d07c76/32.png) [@keyooo2](https://discourse.nodered.org/u/keyooo2)
#### Post date: [30 March 2025 10:32 UTC](https://discourse.nodered.org/t/msg-payload-function-python/96339/1 "2025-03-30T10:32:10Z")

</div>

He needs help. I don't know how to pass in node-re Payload to Function Python variable = NewDoc at a later stage in python I replace an existing line in the auto file

```auto
[
    {
        "id": "1046fdc339d116e8",
        "type": "function",
        "z": "6aa251ec168de374",
        "name": "Budowanie Emial",
        "func": " \nmsg.NewDoc=5555\n \nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 235,
        "y": 300,
        "wires": [
            [
                "f0e14692235cab36"
            ]
        ],
        "l": false
    },
    {
        "id": "f0e14692235cab36",
        "type": "template",
        "z": "6aa251ec168de374",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "python",
        "syntax": "plain",
        "template": "import re\n\n \n\ninput_file = \"C:/Node-red/Automat.js\"\noutput_file = \"C:/Node-red/AutomatN.js\"\n\n# Odczytanie pliku\nwith open(input_file, \"r\") as file:\n lines = file.readlines()\n\n# Modyfikacja linii 213 (indeksy od 0, więc 213)\nif len(lines) >= 198:\n lines[197] = lines[197].replace( \"NewDoc\" ,\"NewDoc\" )\n\nif len(lines) >= 213:\n lines[212] = lines[212].replace( \"NewDoc\",\"NewDoc\" )\n \n# Zapisanie zmodyfikowanego pliku\nwith open(output_file, \"w\", encoding=\"utf-8\") as file:\n file.writelines(lines)\n\nprint(f\"Plik został zapisany jako {output_file}\")\n",
        "output": "str",
        "x": 360,
        "y": 400,
        "wires": [
            [
                "46f9ec1180fbf108"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

### Author: ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)
#### Post date: [30 March 2025 12:29 UTC](https://discourse.nodered.org/t/msg-payload-function-python/96339/2 "2025-03-30T12:29:19Z")

</div>

I have no real idea what you are trying to do but the attached flow replaces "NewDoc" as the 2nd argument in the `replace` function with the supplied value (also the last function in the script I think should be `printf`)

```auto
[{"id":"1046fdc339d116e8","type":"function","z":"506727cd93754bdb","name":"Budowanie Emial","func":" \nmsg.NewDoc=5555\n \nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":875,"y":4500,"wires":[["f0e14692235cab36"]],"l":false},{"id":"f0e14692235cab36","type":"template","z":"506727cd93754bdb","name":"","field":"payload","fieldType":"msg","format":"python","syntax":"mustache","template":"import re\n\ninput_file = \"C:/Node-red/Automat.js\"\noutput_file = \"C:/Node-red/AutomatN.js\"\n\n# Odczytanie pliku\nwith open(input_file, \"r\") as file:\n lines = file.readlines()\n\n# Modyfikacja linii 213 (indeksy od 0, więc 213)\nif len(lines) >= 198:\n lines[197] = lines[197].replace( \"NewDoc\", {{NewDoc}} )\n\nif len(lines) >= 213:\n lines[212] = lines[212].replace( \"NewDoc\", {{NewDoc}} )\n \n# Zapisanie zmodyfikowanego pliku\nwith open(output_file, \"w\", encoding=\"utf-8\") as file:\n file.writelines(lines)\n\nprintf(\"Plik został zapisany jako {output_file}\")","output":"str","x":1000,"y":4500,"wires":[["9327a3f51d0c5c6d"]]},{"id":"9327a3f51d0c5c6d","type":"debug","z":"506727cd93754bdb","name":"Template Test","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1220,"y":4500,"wires":[]},{"id":"0351a361b8a67eda","type":"inject","z":"506727cd93754bdb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":740,"y":4500,"wires":[["1046fdc339d116e8"]]}]

```

---

<div class="post-metadata">

### Author: ![keyooo2](https://avatars.discourse-cdn.com/v4/letter/k/d07c76/32.png) [@keyooo2](https://discourse.nodered.org/u/keyooo2)
#### Post date: [2 April 2025 07:34 UTC](https://discourse.nodered.org/t/msg-payload-function-python/96339/3 "2025-04-02T07:34:55Z")

</div>

Hi,  
Thank you printf solved my problem 🙂

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [1 July 2025 07:35 UTC](https://discourse.nodered.org/t/msg-payload-function-python/96339/4 "2025-07-01T07:35:04Z")

</div>

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