Skip to main content
 首页 » 编程设计

python之使用 csv.reader() 加载和访问数据时获取 AttributeError

2024年12月31日9davidwang456

下面是运行代码时的代码,得到一个 AttributeError: 'NoneType' object has no attribute 'format'。

import csv 
import numpy 
def loadCsv(filename): 
    lines = csv.reader(open(filename,"r")) 
    dataset = list(lines) 
    for i in range(len(dataset)): 
        dataset[i] = [float(x) for x in dataset[i]] 
    return dataset 
filename = 'data1.csv' 
dataset = loadCsv(filename) 
print('Loaded data file {0} with {1} rows').format(filename, len(dataset)) 

请您参考如下方法:

改变这个

print('Loaded data file {0} with {1} rows').format(filename, len(dataset)) 

对此

print('Loaded data file {0} with {1} rows'.format(filename, len(dataset))) 

您收到错误是因为您尝试将 .format 应用于 print 函数,而它是一种需要应用于字符串的方法。