Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从输入文件创建名称列表_Python_Python 3.x - Fatal编程技术网

Python 从输入文件创建名称列表

Python 从输入文件创建名称列表,python,python-3.x,Python,Python 3.x,我想弄清楚如何摆脱错误“strhasnoattributeappend”,这简直是疯了。我需要从输入文件中将这些人的名字添加到名称列表中 输入文件中的典型行将为: 塔梅卡·哈里斯3/4/17 以下是我目前掌握的代码: def process_input(file1, any_dict, any_set): #open input file customer_input_file = open(file1, 'r') #initialize a list to sto

我想弄清楚如何摆脱错误“strhasnoattributeappend”,这简直是疯了。我需要从输入文件中将这些人的名字添加到名称列表中

输入文件中的典型行将为:

塔梅卡·哈里斯3/4/17

以下是我目前掌握的代码:

def process_input(file1, any_dict, any_set):

    #open input file
    customer_input_file = open(file1, 'r')


    #initialize a list to store info by line
    line_list = []

    #Create a name counter
    index = 0

    #read file line by line
    for line in customer_input_file:
        #split the line
        line_list = line.split()
        #slice the list to make a list with only names and no dates
        name_list = line_list[-1]
        #create a variable for first name from list
        fname = name_list[0].title()
        #create a last name variable from list
        lname = name_list[1].title()
        #add the two name variables together as a new list
        input_name = [fname + ' ' + lname]


        #add names to name list
        name_list.append(input_name)

        #add names to name list
        any_set.add(name_list[index])

        #count the times a name repeats in the name list
        any_dict[input_name] = name_list.count(input_name)


        #add 1 to index
        index += 1


    #Close input file
    customer_input_file.close
上面返回列表中的最后一项,它是一个字符串,这就是为什么
name\u list.append(input\u name)
抛出错误的原因

您需要排除最后一项:

name_list = line_list[:-1]

非常感谢,错误已经消失了。。。虽然现在它为不可损坏的列表(input_name)抛出了一个TypeError,并且当我删除input_name周围的括号时,我的索引超出了范围:/
name_list = line_list[:-1]