检查字符串是否可转换为整数

Woo*_*per 0 python

我想检查 lambda 表达式中的字符串是否可以转换为整数。

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]])
Run Code Online (Sandbox Code Playgroud)

我检查了这个问题,但没有帮助:

>>> isinstance(1448379000595770,int)
True
>>> isinstance('1448379000595770',int)
False
Run Code Online (Sandbox Code Playgroud)

elz*_*ell 5

在普通函数中,最好的方法是:

def canBeNumber(n):
    try:
        int(n)
        return True
    except ValueError:
        # Not a number
        return False
Run Code Online (Sandbox Code Playgroud)