1

I have data that has strings with floating numbers like

"['0.0'" and '82.00.0' and '82.0\n'

and I only want to extract floating points till two/one decimal points like so-

"['0.0'" and '82.00.0' and '82.0\n' to 0.0, 82.0, 82.0

The data structure is a big quoted CSV like:

"0.0, 82.00.0,...., 82.0\n"

I'm iterating through these to store them to the temp

tempprices.split(',') temp =[] for n in range(l, len(tempprices)-1): temp.append(map(ast.literal_eval,re.findall(r'(?<!\S)[+-]?\d+\.\d{1,2}(?!\.*\d)',tempprices[n])))

where l is some index value.

I want to append these to temp like so [0.0, 82.0, 82.0]

How to achieve that?

Sushant
  • 3,499
  • 3
  • 17
  • 34

2 Answers2

1

There are several issues:

  • You did not assign the split chunks to a variable (see tempprices.split(','))
  • You actually have to extract 1 value, so the re.sub can work, but a re.search is safer

You may use the following fix:

import re

tempprices = "0.0, 82.00.0,...., 82.0\n"
cells = tempprices.split(',')
temp =[]
for t in cells:
    mObj = re.search(r'-?\d+\.\d{1,2}', t)
    if mObj:
        temp.append(float(mObj.group()))
print(temp)

See the IDEONE demo

If you can have multiple float values inside each cell, you will have to use ast.literal_eval with re.findall:

for t in cells:
    temp.extend(map(ast.literal_eval, re.findall(r'-?\d+\.\d{1,2}', t)))
         ^^^^^^ ^^^^^^^^^^^^^^^^^^^^

See another IDEONE demo

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thank you so much for your time. This finally worked, and I had split the values, just stored them in the same variable. But thanks :) – Sushant Jun 13 '16 at 12:52
  • Glad it worked for you. Please also consider upvoting if my answer proved helpful to you (see [How to upvote on Stack Overflow?](http://meta.stackexchange.com/questions/173399/how-to-upvote-on-stack-overflow)). – Wiktor Stribiżew Jun 13 '16 at 13:25
0

Try this:

import re

s = '82.00\n    1.3000   9.1'
result = re.findall(r'\d+\.\d{1,2}', s)
print result

Output: ['82.00', '1.30', '9.1']

Gal Dreiman
  • 3,969
  • 2
  • 21
  • 40