To remove all characters after the first comma, including the comma itself, from the line:
s = 'a,b,c' before_comma = s.partition(',')[0] # -> 'a'
All (!) Answers given at the moment, except s.split(',', 1)[0]
do not cope with this simple task, for example:
s[:s.find(',')]
can silently return a wrong answer if there are no commas in the line. The correct solution should handle the case s.find(',') == -1
, that is, when ',' not in s
:
i = s.find(',') before_comma = s[:i] if i != -1 else s
re.sub(r',\d+', '', MyString)
removes commas followed by digits, leaving all other characters. To remove all characters after a comma using a regular expression:
import re before_comma = re.sub(',.*', '', s, flags=re.DOTALL)
that is, an analogue of the ",*"
pattern is the ',.*'
regular expression.
To remove arbitrary punctuation, you can use r"\p{P}+"
regex: How to find characters from the tuple ('!', ',', '?') In the string and delete them?
If the input data is in Excel, then you should make sure that the floating-point numbers are exported to csv using a period rather than a comma as a separator - this can help to correctly round the result if the data is treated as numbers, not strings. Or you can use libraries that allow you to work with Excel files (for example: xlrd
, openpyxl
) or Excel itself directly .