0

I wanted to check whether a string is convertible to an integer inside lambda expression.

import re,time
rdd = sc.textFile("file:///home/vdpqa/sample.gz")
new1 = rdd.map(lambda x: re.split('/|\.|\|',x))
    .filter(lambda arr: (len(arr) > 9) and isinstance(arr[7],int)).map(lambda x: x[:9])

new = new1.map(lambda x: [x[0],x[1],x[3],
        time.strftime('%Y%m%d', time.localtime(int(x[7])/1000000)),x[8]])

I checked this question but it didn't help:

>>> isinstance(1448379000595770,int)
True
>>> isinstance('1448379000595770',int)
False
Community
  • 1
  • 1
WoodChopper
  • 4,265
  • 6
  • 31
  • 55

1 Answers1

5

In an ordinary function the best approach would be:

def canBeNumber(n):
    try:
        int(n)
        return True
    except ValueError:
        # Not a number
        return False
mka
  • 153
  • 5
elzell
  • 2,228
  • 1
  • 16
  • 26