【问题标题】:PYTHON - Writing a list to a file so it can be read and written toPYTHON - 将列表写入文件以便可以读取和写入
【发布时间】:2015-05-20 19:31:27
【问题描述】:

我是一个完整的新手程序员,并且正在制作一个基本的“会员”计划,以给我一些函数和列表方面的经验 到目前为止我有this:

#Club membership program with functions

Club = ['George','Isaiah','Alby','Jack']

#FUNCTIONS=========================================================================================================================================================================================================

def remove_member():
    """Function to delete member from the club"""
    print('The club members are:', Club)
    removal_member = str(input('Enter the name of the member you would like to remove:'))
    if removal_member in Club:
        Club.remove(removal_member)
    else:
        print('Member not found')
    print('The club members are now:', Club)

def add_member():
    """Funtion to add a member to the club"""
    new_member = str(input('Enter the name of the member you would like to add:'))
    Club.append(new_member)
    print('The club members are now:', Club)

def functions():
    print('''
   List of current funtions:
   1 = Remove a member
   2 = Add a member
   3 = trherjuthyjht\n''')

#MAIN==================================================================================================================================================================================================================

print('HELLO AND WELCOME TO THE CLUB MEMBERS PROGRAM')
print('The current members of the club are:', Club)
functions()


run_program = input('Would you like to carry out a function? (y/n):')
while run_program == 'y':
    function_no = int(input('enter the the number of the funtion you would like to execute:'))
    if function_no == 1:
        remove_member()
        run_program = input('Would you like to carry out a function? (y/n):')
        functions()
    else:
        if function_no == 2:
            add_member()
            run_program = input('Would you like to carry out a function? (y/n):')
            functions()

input('Thanks you for using the Club membership program, press enter to exit.')

接下来我想做的是扩展我的文件处理技能: 我希望能够将列表写入文件,以便每次打开或关闭程序时都可以检索列表

  1. 我该怎么做
  2. 我需要将它保存为什么文件类型,以便 python 将其识别为列表

任何进一步的帮助将不胜感激

【问题讨论】:

  • 下一次,请在您的帖子中包含您的代码,而不是在外部链接中。这次我把它编辑到你的帖子里了
  • 有许多不同的格式可以写入文件。您是否要将数据写入文件,例如文件的每一行都有一个名称,然后解析输入?还是您真的要保存数据结构列表及其所有成员?
  • @GC123UNC 该文件纯粹是供程序使用的,因此人类不需要使用,因此无论程序何时检索下次打开程序时的列表,最好的方法都是

标签: python file


【解决方案1】:

您所描述的是数据序列化。格式很多。

例如,看看pickle 模块。示例用法:

 >>> import pickle
 >>> l = ["my","list","of","strings"]
 >>> pickle.dump(l,open('pickledfile.txt','wb'))    
 >>> pickle.load(open('pickledfile.txt','rb'))
    ['my', 'list', 'of', 'strings']

【讨论】:

    【解决方案2】:

    这里有两个函数可以保存和加载您的列表:

    Club = ['George','Isaiah','Alby','Jack']
    def saveClub(club):
        myfile= open('myfile.txt','w')
        for member in club:
            myfile.write(member+'\n')
    
    def loadClub():
        newClub=[]
        with open('myfile.txt', 'r') as ins:
            for line in ins:
                newClub.append(line.rstrip('\n'))
        return newClub
    
    saveClub(Club)
    Club=loadClub()
    

    【讨论】:

    • 你能帮我分析一下发生的事情吗,以帮助我理解,谢谢:)
    • 您应该阅读这篇编写良好且简单的关于在 Python 中读取和写入文件的教程:pythonforbeginners.com/files/…
    猜你喜欢
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多