Search code examples
pythoncsvstripstripping

Remove comma from middle of string in python


My problem is stripping a comma out from a string. This string contains two commas and when this second comma is included in my list of strings, it interferes when writing to a CSV in the proper manner.

The string I would like to strip the comma from looks can occur in two forms such as,

'Warning, triggered', 
'Fatal, on',

As you can see this comma thats is in between the two words will effect my script when writing to a CSV. I am unsure how to go about stripping this comma from these two possible string outputs. I use these strings and populate a dictionary of lists. This dictionary is then used to create a CSV file however this comma I pointed out does not allow me to do correctly.

My goal is to strip the two possible string outputs to look like:

'Warning triggered',
'Fatal on',

I can provide more detail if needed. Thanks


Solution

  • You can remove the comma as follows.

    my_str = 'Warning, triggered'
    my_str_without_comma = my_str.replace(',', '')
    

    But this sounds like an XY problem where you have some data that you are trying to convert to CSV format and you have already taken an approach for it. Try to ask the actual problem you are trying to solve.