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
Webdriverexception: Message: Service Chromedriver Unexpectedly Exited. Status Code Was: 127
Why Is Python No Longer Waiting for Os.System to Finish
Python Divide by Zero Encountered in Log - Logistic Regression
Add Missing Dates to Pandas Dataframe
Convert String from Big-Endian to Little-Endian or Vice Versa in Python
Number of Common Letters in Two Strings
Calculate Angle (Clockwise) Between Two Points
How to Get the Url of the Active Google Chrome Tab in Windows
Algorithm for Distributing a Number Between Certain Number of Chunks
Python: Using Doctests for Classes
How to Remove Additional Commas in a List in Python
Create a New Dataframe Based on Rows With a Certain Value
Running a Powershell Cmdlet in a Python Script
How to Append Dataframes Inside a for Loop in Python
Read CSV from Google Cloud Storage to Pandas Dataframe
Accuracy Score Valueerror: Can't Handle Mix of Binary and Continuous Target