Home > Software design >  Python - remove specific line in string variable
Python - remove specific line in string variable

Time:09-22

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, ""))

CodePudding user response:

You can replace the line using re module:

re.sub(r'\s*cachedLength=".*"', '', input_columns)

But as mentioned in the comments, technically, Python will recreate the string because it's an immutable type.

CodePudding user response:

Not exactly answering the question, but if modifying the xml is what you're after, you could use a package for that. E.g., using lxml:

xml = """\
<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}]" />
"""

e = etree.fromstring(xml)

del e.attrib["cachedLength"]

print(etree.tostring(e, pretty_print=True, encoding="unicode"))
  • Related