简体   繁体   English

Python TypeError: (“'NoneType' 类型的对象没有 len()”

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

I ran into the error when running executable converted from python program using Pyinstaller.使用 Pyinstaller 运行从 python 程序转换的可执行文件时遇到错误。 It was error-free when running directly from python program.直接从 python 程序运行时没有错误。 Appreciate any guidance!感谢任何指导!

Snippet of python codes: python代码片段:

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:运行 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".对于输出,“NRIC_first”下将有所有案例的条目,但“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.对于您正在处理的某些内容,x 必须为 None。 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.该错误不是由于 NRIC_second 列中的空字符串("" 是空字符串)造成的,因为空字符串的长度为 0。该列中可能有一些 None 值。 The dataset needs to be cleaned up.需要清理数据集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python-TypeError:“ NoneType”类型的对象没有len() - Python - TypeError: object of type 'NoneType' has no len() Python TypeError:“NoneType”类型的对象没有 len() - Python TypeError: object of type 'NoneType' has no len() Python:TypeError:'NoneType' 类型的对象没有 len() - Python: TypeError: object of type 'NoneType' has no len() TypeError:类型为&#39;NoneType&#39;的对象没有len()python - TypeError: object of type 'NoneType' has no len() python Python错误“ TypeError:类型为&#39;NoneType&#39;的对象没有len() - Python error "TypeError: object of type 'NoneType' has no len() Python 错误 - TypeError:“NoneType”类型的 object 没有 len() - Python error - TypeError: object of type 'NoneType' has no len() Python:TypeError:“NoneType”类型的 object 没有 len():使用 JSON 和 rncryptor - Python: TypeError: object of type 'NoneType' has no len(): Using JSON And rncryptor TypeError:“NoneType”类型的对象在应用程序部署中没有 len() Python - TypeError: object of type 'NoneType' has no len() Python on application deployment Python tkinter TypeError:类型为“ NoneType”的对象没有len() - Python tkinter TypeError: object of type 'NoneType' has no len() Python 错误:“TypeError:“NoneType”类型的 Object 没有 len()” - Python error: “TypeError: Object of type 'NoneType' has no len()”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM