f = open("Information.txt","w+",encoding="utf8") Information = [["1","alex","IT"],["2","bob","IT"]] User_Choice = int(input("Your choice is:")) for i in Information: if User_Choice == int(i[0]): Information.remove(Information[User_Choice]) for j in Information: f.write(','.join(j).strip()) #关键是这一步,把列表通过join方法转换成字符串,然后再写入文件 f.close()
该程序的作用是用户输入数字后,会删掉列表中对应的元素,然后再把列表中剩余的元素写入文件中