replace

Regex string parsing: pattern starts with ; but can end with [;,)%&@]

Regex string parsing: pattern starts with ; but can end with [;,)%&@] Question: I am attempting to parse strings using Regex. The strings look like: Stack;O&verflow;i%s;the;best! I want to parse it to: Stack&verflow%sbest! So when we see a ; remove everything up until we see one of the following characters: [;,)%&@] (or replace with empty …

Total answers: 1

Replacing a SimpleString inside a libcst function definition? (dataclasses.FrozenInstanceError: cannot assign to field 'body')

Replacing a SimpleString inside a libcst function definition? (dataclasses.FrozenInstanceError: cannot assign to field 'body') Question: Context While trying to use the libcst module, I am experiencing some difficulties updating a documentation of a function. MWE To reproduce the error, the following minimal working example (MWE) is included: from libcst import ( # type: ignore[import] Expr, …

Total answers: 1

Search and replace is adding not replacing

Search and replace is adding not replacing Question: I am trying to find this string in a file: /home/pi/dew-heater/get-temp.py And replace it with #/home/pi/dew-heater/get-temp.py But there are cases where it already reads #/home/pi/dew-heater/get-temp.py. So my program is replacing it with a double ## so it looks like this: ##/home/pi/dew-heater/get-temp.py How can I prevent the double …

Total answers: 1

What is the most pythonic way of replacing values in nested lists?

What is the most pythonic way of replacing values in nested lists? Question: I have a list of strings, and I want to replace each ‘X’ and ‘O’ with True and False – turning each string into a list of booleans. arr = [‘XOOXO’, ‘XOOXO’, ‘OOOXO’, ‘XXOXO’, ‘OXOOO’] temp_list = [] bool_lists =[] for str …

Total answers: 2

read file and replace substrings in python

read file and replace substrings in python Question: I am trying to read a text file with python and remove certain strings from it. I have written a script, but for some reason it does not do what I would expect. As a minimal example I have the following text file named test.txt Here some …

Total answers: 2

How do I replace slaces in python

How do I replace slash character "'' by backslash character "/" in python Question: I need to replace forward slash "" with backward slash "/". I have tried: c = "/r/tes/integrtest_Ame.txt" x = c.replace("", "/") print(x) But I have got the error: File "C:Usersbwar1korDesktopTesttest.py", line 33 x = c.replace("", "/") ^ SyntaxError: EOL while scanning …

Total answers: 1

Replacing a word in a file to add another file content

Replacing a word in a file to add another file content Question: Input file (csv). NAME,TYPE,ACCESS_TOKEN dev765,datatype,Token765 dev879,datatype,Token879 Append file: { "file": "string", "mapping": { "columns": [ { "type": "NAME" }, { "type": "TYPE" }, { "type": "ACCESS_TOKEN" } ], "delimiter": ",", "header": true, "update": true } } The goal is to replace the "string" …

Total answers: 1