Cleaning many string variables at once with strip()

Question:

I’m trying to get rid of two special character combinations from all my variables with strip.
is there a better way to do this with a loop?

currentdisk = currentdisk.strip("└─")
currentdisk = currentdisk.strip("├─")
currentmpath = currentmpath.strip("└─")
currentmpath = currentmpath.strip("├─")
currentpartition = currentpartition.strip("└─")
currentpartition = currentpartition.strip("├─")
volumegroup = volumegroup.strip("└─")
volumegroup = volumegroup.strip("├─")
logicalvolume = logicalvolume.strip("└─")
logicalvolume = logicalvolume.strip("├─")
mountpoint = mountpoint.strip("└─")
mountpoint = mountpoint.strip("├─")
Asked By: Ricardo

||

Answers:

  1. Group all variables in a dict.
  2. Iterate over with your 2 strip operations.
  3. Get each variable by key.
# 1. fill the dict
my_values = {
    'currentdisk': currentdisk,
    'currentmpath': currentmpath,
    'currentpartition': currentpartition,
    'volumegroup': volumegroup,
    'logicalvolume': logicalvolume,
    'mountpoint': mountpoint
}

# 2. strip
for k in my_values.keys():
    my_values.update({k: my_values[k].strip('└─├─')})

# 3. access a value by key
currentdisk = my_values.get('currentdisk')
Answered By: hc_dev
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.