How to Strip Comma in Python String

How to strip comma in Python string

You want to replace it, not strip it:

s = s.replace(',', '')

Remove comma from middle of string in python

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.

How do I strip the comma from the end of a string in Python?

When you say

awk = subprocess.Popen([r"awk", "{print $11}"], stdin=subprocess.PIPE)
awk_stdout = awk.communicate(uptime_stdout)[0]

then the output of the awk process is printed to stdout (e.g. a terminal).
awk_stdout is set to None. awk_stdout.rstrip('\n') raises an AttributeError because None has no attribute called rstrip.

When you say

awk = subprocess.Popen([r"awk", "{print $11}"], stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
awk_stdout = awk.communicate(uptime_stdout)[0]

then nothing is printed to stdout (e.g. the terminal), and awk_stdout gets the output of the awk command as a string.

python strip a comma at the beginning

If you have the following as one string item:

800,989, hi, hello

,900,108, bye, bye

You can split the string in a list of lines first, lstrip each line, and then rejoin the list into a string:

'\n'.join(i.lstrip(',') for i in item.splitlines())

How to remove commas from ALL the column in pandas at once

Numeric columns have no ,, so converting to strings is not necessary, only use DataFrame.replace with regex=True for substrings replacement:

df = df.replace(',','', regex=True)

Or:

df.replace(',','', regex=True, inplace=True)

And last convert strings columns to numeric, thank you @anki_91:

c = df.select_dtypes(object).columns
df[c] = df[c].apply(pd.to_numeric,errors='coerce')

Python Remove Comma In Dollar Amount

You could use replace to remove all commas:

"10,000.00".replace(",", "")

Python - Comma in string causes issue with strip

It seems like you're looking for the behaviour of replace(), rather than strip().

Try using replace('"', '') instead of strip('"'). strip only removes characters from the beginning and end of strings, while replace will take care of all occurrences.

Your example would be updated to look like this:

example = [('7-30-17','0x34','"Upload Complete"'),('7-31-17','0x35','"RCM","Interlock error"')]

example = [(x,y,(z.replace('"', '')))
for x,y,z in example]

example ends up with the following value:

[('7-30-17', '0x34', 'Upload Complete'), ('7-31-17', '0x35', 'RCM,Interlock error')]


Related Topics



Leave a reply



Submit