Ricardo
Ricardo

Reputation: 3

Cleaning many string variables at once with strip()

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("├─")

Upvotes: 0

Views: 73

Answers (1)

hc_dev
hc_dev

Reputation: 9418

  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')

Upvotes: 0

Related Questions