Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #87380 > unrolled thread
| Started by | Larry Martell <larry.martell@gmail.com> |
|---|---|
| First post | 2015-03-13 14:38 -0400 |
| Last post | 2015-03-13 14:38 -0400 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: regex help Larry Martell <larry.martell@gmail.com> - 2015-03-13 14:38 -0400
| From | Larry Martell <larry.martell@gmail.com> |
|---|---|
| Date | 2015-03-13 14:38 -0400 |
| Subject | Re: regex help |
| Message-ID | <mailman.335.1426271938.21433.python-list@python.org> |
On Fri, Mar 13, 2015 at 1:29 PM, MRAB <python@mrabarnett.plus.com> wrote: > On 2015-03-13 16:05, Larry Martell wrote: >> >> I need to remove all trailing zeros to the right of the decimal point, >> but leave one zero if it's whole number. For example, if I have this: >> >> >> 14S,5.0000000000000000,4.56862745000000,3.7272727272727271,3.3947368421052630,5.7307692307692308,5.7547169811320753,4.9423076923076925,5.7884615384615383,5.137254901960000 >> >> I want to end up with: >> >> >> 14S,5.0,4.56862745,3.7272727272727271,3.394736842105263,5.7307692307692308,5.7547169811320753,4.9423076923076925,5.7884615384615383,5.13725490196 >> >> I have a regex to remove the zeros: >> >> '0+[,$]', '' >> >> But I can't figure out how to get the 5.0000000000000000 to be 5.0. >> I've been messing with the negative lookbehind, but I haven't found >> one that works for this. >> > Search: (\.\d+?)0+\b > Replace: \1 > > which is: > > re.sub(r'(\.\d+?)0+\b', r'\1', string) Thanks! That works perfectly.
Back to top | Article view | comp.lang.python
csiph-web