0

Let's suppose I have a variable called data. This data variable has all this data and I need to remove certain parts of it while keeping most of it. Let's say I needed to remove all the ',' (commas) in this data variable. How would I write a script that would analyze that data and then remove those commas?

Code Example:

    data = '''
data,data,data,data,data,data
data,data,data,data,data,data
'''
Noah R
  • 5,287
  • 21
  • 56
  • 75

2 Answers2

5

Just replace them:

data = data.replace(',', '')

If you have more characters, try using .translate():

data = data.translate(None, ',.l?asd')
Blender
  • 289,723
  • 53
  • 439
  • 496
  • But then I have a lot of ' ' in my data variable and I can't have that. What should I add to take these ' ' out? – Noah R Mar 21 '12 at 01:34
  • If you mean the characters `''` will appear in your string...no they won't. That's the empty string; the quote marks just mark the start and end of it. – Taymon Mar 21 '12 at 01:35
0
def remove_chars(data, chars):
    new_data = data
    for ch in chars:
        new_data = new_data.replace(ch, '')
    return new_data

Example:

>>> data = 'i really dont like vowels'
>>> remove_chars(data, 'aeiou')
' rlly dnt lk vwls'
juliomalegria
  • 24,229
  • 14
  • 73
  • 89