There are numbers, for example, 130203.93
It should work: 130203

In Excel, it is removed by a simple template to find and replace: ", *" - the comma and everything after it will be deleted. How to do the same in .replace ?

I tried this:

 .replace(",*" , "") 

Did not work out.

Which character in "regular" expressions replaces * with "all that comes after"?
Or how to indicate in the same replace so that several any characters are deleted, for example, 2 decimal points and including a comma?

Thank!

    7 answers 7

    I was helped by such code .split (",") [0]

    As I understood it divides the string into two parts, and returns the first piece to the comma, that is, [0]. But the question is open how to do it in replace :)

    • And why replace if it works as it should? - Vladimir Martyanov
    • one
      New knowledge is interesting - and of course I don’t know why or the better. - Amaroc
    • You can pass the maxsplit parameter in order not to split the string unnecessarily: s.split(',', 1)[0] - jfs

    If the task is precisely to remove the first comma and all that comes after it, that will work:

     s = "abcd1234,5678" print s[:s.find(',')] 

    Regexp for your task - surely brute force.

    • Tell me please, why bust? Are they too complicated to understand for a beginner or will they load the system? I would just like to know this grail is an analogue of an asterisk * in Excel :) - Amaroc
    • Difficult, for example, you could not immediately write it, someone else might not understand. And in terms of "to find the symbol and cut everything off," you perfectly described the task yourself, the implementation remained. The code for working with regexp inside is huge and takes time to execute. Well, why is it difficult to do, if you can just? - Vladimir Martyanov
    • Thanks, it works, tell me why you have no brackets after the print () - I saw this in the video lessons. I have it on Windows and in Linux does not roll. It depends on the version of the python or some settings? - Amaroc
    • @Amaroc, yes, I’m out of habit for the second code I am writing, in the third version brackets are required. - Vladimir Martyanov
    • for some reason, your version did not dock with my code. But with split everything is ok. An example of this: money.replace ("\ xa0", "") .split (",") [0] - here I also remove some spaces in the numbers, and then commas. - Amaroc

    We connect the module for working with regular expressions:

     import re 

    Replace:

     MyString = re.sub(r',\d+', '', MyString) 

    Regular set without opening and closing a slash, but with r in front of the line. The second argument is a string (not a regular!) To replace. This regular schedule cuts off all the numbers after the decimal point, along with it. Other characters will be ignored, but for working with string representations of numbers this is sufficient.

    And .replace , now (I don't know how it was before), does not work with regular expressions, only with simple strings.

    More about the re module here .

    • the question asks to remove all characters, not just the numbers ( \d ) after the comma. - jfs
    • @jfs at the beginning of the question very clearly indicated the original data: there is a number . In the number of any other characters except numbers, after the comma can not be, otherwise it is already a string . - Risto

    Use floor () from the math module.

     math.floor(130203.93) 
    • Thank you, but this is not about rounding (as I understand it), but about converting data into the form that is suitable for both Excel and Google is spreading (as far as I remember, one eats commas and another point - which cannot but irritate), therefore habits from commas and points in the "money" I try to get rid of. This is in a particular case - but in general it is interesting to find out exactly the special character that means "everything after that" in Excel it is *, and in python? - Amaroc
    • @Amaroc: math.floor(f) does not round. This function returns the largest integer less than or equal to f (different from int(f) ). That would be a possible answer if you had floating-point numbers at the input, not strings. - jfs

    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 .

    • Tactical DownVoting is a two-way technique. - Risto
    • @Risto: I do not use "Tactical DownVoting" (whatever that means). I minus the answer if I don’t like the answer and the author refuses to improve it (I don’t care who the author is, if the answer is normal). - jfs
    • So you, quite by chance, “didn't like” all the answers, with a rating higher than yours? - Risto
    • @Risto: your assumption that I voted "against" all the answer [s], with a rating higher than yours, " is not true. - jfs
    • @Risto: any user with a 1000+ reputation ( allows you to see voting details ) can verify the veracity of my comment. If you did not know about it, then I am waiting for an apology for false accusations against me. - jfs

    If you need to work with numbers, then to truncate the fractional part, you can use the trunc function from the math module.

     import math math.trunc(123.456) math.trunc(789) 

      If there is a number in the string representation of the format "12345.123", then to discard the numbers after the comma, int(float("12345.123")) = 12345 enough

      • The question is put so that it is necessary to convert the number - Dinar Gataullin