Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #87372 > unrolled thread
| Started by | Larry Martell <larry.martell@gmail.com> |
|---|---|
| First post | 2015-03-13 12:05 -0400 |
| Last post | 2015-03-14 11:48 +1100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
regex help Larry Martell <larry.martell@gmail.com> - 2015-03-13 12:05 -0400
Re: regex help Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-13 18:10 +0100
Re: regex help Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-14 11:48 +1100
| From | Larry Martell <larry.martell@gmail.com> |
|---|---|
| Date | 2015-03-13 12:05 -0400 |
| Subject | regex help |
| Message-ID | <mailman.329.1426262765.21433.python-list@python.org> |
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.
[toc] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-03-13 18:10 +0100 |
| Message-ID | <2145563.lrZ297Te1a@PointedEars.de> |
| In reply to | #87372 |
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. First of all, I find it unlikely that you really want to solve your problem with regular expressions. Google “X-Y problem”. Second, if you must use regular expressions, the most simple approach is to use backreferences. Third, you need to show the relevant (Python) code. <http://www.catb.org/~esr/faqs/smart-questions.html> -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2015-03-14 11:48 +1100 |
| Message-ID | <55038554$0$12988$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #87372 |
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.
def strip_zero(s):
if '.' not in s:
return s
s = s.rstrip('0')
if s.endswith('.'):
s += '0'
return s
And in use:
py> strip_zero('-10.2500')
'-10.25'
py> strip_zero('123000')
'123000'
py> strip_zero('123000.0000')
'123000.0'
It doesn't support exponential format:
py> strip_zero('1.2300000e3')
'1.2300000e3'
because it isn't clear what you intend to do under those circumstances.
--
Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web