简体   繁体   中英

Python TypeError: (“object of type 'NoneType' has no len()”

I ran into the error when running executable converted from python program using Pyinstaller. It was error-free when running directly from python program. Appreciate any guidance!

Snippet of python codes:

def post_processing(df):
    column_names = df.columns.tolist()
    def update_nric_checksum(x):
        w_list = [0,2,7,6,5,4,3,2]
        alp_dict = {1: 'A', 2:'B', 3:'C', 4:'D', 5:'E', 6:'F', 7:'G', 8:'H', 9:'I', 10:'Z', 11:'J'}
        key_alp = []
        key_alp_list = []
        total = 0
        
        if (len(x)<=7) or (len(x)>9):
            return x

        if len(x)==9:
            for idx, item in enumerate(x[:-1]):
                if item in string.digits:
                    total += (int(item) * w_list[idx])
            remainder = total%11
            key_alp = 11 - remainder
            updated_x = x[:8] + alp_dict[key_alp]
            return updated_x
    
    df = df.fillna("").astype(str)
    
    df[['NRIC_first','NRIC_second']]=df['NRIC'].str.split(",", expand=True) 
    df['NRIC_first']=df['NRIC_first'].str.replace("[","").str.replace("]","").str.replace("'","").str.replace(" ","")
    df['NRIC_second']=df['NRIC_second'].str.replace("[","").str.replace("]","").str.replace("'","").str.replace(" ","")
    df[['NRIC_first_','NRIC_second_']] = df[['NRIC_first','NRIC_second']].applymap(update_nric_checksum) 
    df.loc[(df.NRIC_first_==df.NRIC_second_), 'NRIC_'] = "['" + df['NRIC_first_'].map(str) + "']"
    df.loc[(df.NRIC_first_!=df.NRIC_second_), 'NRIC_'] = "['" + df['NRIC_first_'].map(str) + "', '" + df['NRIC_second_'].map(str) + "']"
    df.drop(['NRIC', 'NRIC_first','NRIC_second','NRIC_first_','NRIC_second_'], axis=1, inplace = True)
    df = df.rename(columns={'NRIC_': 'NRIC'})
return df[column_names]

Snippet of error from running exe:

Traceback (most recent call last):
  File "MyCodes.py", line 319, in <module>
  File "utils\util.py", line 124, in post_processing
    df[['NRIC_first_','NRIC_second_']] = df[['NRIC_first','NRIC_second']].applymap(update_nric_checksum)
  File "site-packages\pandas\core\frame.py", line 6553, in applymap
  File "site-packages\pandas\core\frame.py", line 6487, in apply
  File "site-packages\pandas\core\apply.py", line 151, in get_result
  File "site-packages\pandas\core\apply.py", line 257, in apply_standard
  File "site-packages\pandas\core\apply.py", line 286, in apply_series_generator
  File "site-packages\pandas\core\frame.py", line 6551, in infer
  File "pandas/_libs/lib.pyx", line 2217, in pandas._libs.lib.map_infer
  File "utils\util.py", line 79, in update_nric_checksum
    if (len(x)<=7) or (len(x)>9):
TypeError: ("object of type 'NoneType' has no len()", 'occurred at index NRIC_second')
[12272] Failed to execute script MyCodes

For output, there will be entries for all cases under "NRIC_first", but it may be empty for "NRIC_second". This is how the input looks like:

NRIC
['SXXXXXXXB', 'SXXXXXXXI']
['SXXXXXXXH']
['SXXXXXXXB', 'SXXXXXXXC']
['SXXXXXXXH']

x must be None for some of the things you are processing. Add something like

if math.isnan(x):
    return "0"

and the error will go away. Fundamentally, it seems like your dataset might need to be cleaned up.

The error is not due to empty strings("" is the empty string) in NRIC_second column, because length of empty string is 0. There might be some None values in that column. The dataset needs to be cleaned up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM