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

2 Answers 2

5

Just replace them:

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

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

data = data.translate(None, ',.l?asd')
Sign up to request clarification or add additional context in comments.

2 Comments

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?
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.
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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.