天天看点

AttributeError: 'NoneType' object has no attribute 'format'

今天写代码的时候出现了一个及其简单的问题,百度很久都没找到是什么原因,最后才发现是print 函数少了个括号。

# -*- encoding:utf-8 -*-
from csv import reader


# Load a CSV file
def load_csv(filename):
    file = open(filename, "r")
    lines = reader(file)  # 此时reader返回的值是csv文件中每行的列表,将每行读取的值作为列表返回
    dataset = list(lines)  # 把filename文件返回的迭代的器对象lines列表化
    return dataset


filename = 'pima-indians-diabetes.csv'
dataset = load_csv(filename)
#  少括号输出
# print('加载文件 {0} 有 {1} 行 {2} 列').format(filename, len(dataset), len(dataset[0]))
# 正确输出
# print(('加载文件 {0} 有 {1} 行 {2} 列').format(filename, len(dataset), len(dataset[0])))
           

然后就出现了下面的:

AttributeError: ‘NoneType’ object has no attribute ‘format’

问题

D:\dev\python.exe "D:/pycharm/code/statisticsLearning/Load _CSV_File.py"
Traceback (most recent call last):
  File "D:/pycharm/code/statisticsLearning/Load _CSV_File.py", line 15, in <module>
    print('加载文件 {0} 有 {1} 行 {2} 列').format(filename, len(dataset), len(dataset[0]))
AttributeError: 'NoneType' object has no attribute 'format'
加载文件 {0} 有 {1} 行 {2} 列

           
在python3当中是需要print()才能输出
#错误代码
print('加载文件 {0} 有 {1} 行 {2} 列').format(filename, len(dataset), len(dataset[0]))
           

希望对有此疑惑的人有所帮助