0

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

Shashank V
  • 10,007
  • 2
  • 25
  • 41
cinemod
  • 27
  • 1
  • 7
  • 1
    Are you using something like [CSV](https://docs.python.org/3/library/csv.html) to produce the csv files? – Mark Jan 03 '20 at 18:20
  • Yes i am, the CSV writing is working fine, its the formatting I am struggling with because of this singular comma that would occur in either of the strings. – cinemod Jan 03 '20 at 18:24
  • The `csv` module will normally quote strings containing a comma, so you should provide your code to see what is going wrong. replacing the comma isn't the answer. – Mark Tolonen Jan 10 '20 at 15:27

1 Answers1

0

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.

Shashank V
  • 10,007
  • 2
  • 25
  • 41