Home:ALL Converter>Python - remove specific line in string variable

Python - remove specific line in string variable

Ask Time:2021-09-22T05:31:21         Author:marcin2x4

Json Formatter

Is it possible in Python to remove only single line from a string variable without doing the concatenation of both chunks? I'm after removing cachedLength line.

input_columns = f'''\
<inputColumn 
    refId="Package\DFT\DST.Inputs[DST Input].Columns[{tbl}]"
    cachedDataType="{dt_type}"
    cachedName="{tbl}"
    cachedLength="{dt_lngh}"
    cachedPrecision="{dt_prc}"
    cachedScale="{dt_scl}"
    externalMetadataColumnId="Package\DFT\DST.Inputs[DST Input].ExternalColumns[{tbl}]"
    lineageId="Package\DFT\SRC.Outputs[SRC Output].Columns[{tbl}]" />
'''

Work in progress:

i = input_columns.split("\n")[4]
print(input_columns.replace(i, ""))

Author:marcin2x4,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69275777/python-remove-specific-line-in-string-variable
Yann :

You can replace the line using re module:\nre.sub(r'\\s*cachedLength=".*"', '', input_columns)\n\nBut as mentioned in the comments, technically, Python will recreate the string because it's an immutable type.",
2021-09-21T22:00:40
yy