Making Python list in file and editing it later
Question: I'm making an program which stores certain data in txt files. I did it like
this and it works, but I want to change it to lists, so it's easier to edit
stuff inside with list.remove and list.append... This is format I made:

    
    
    Username:data1:data2:data3:data++
    

What I want to achieve is:

    
    
    Username:[data1, data2, data3, data++]
    

Why I want this?

Because editing data separated with ":" is not "Pythonic way" to do this, so I
want to convert them to lists...

I get certain lines using this filter:

    
    
    textfile = open("%s/CreativePoints/Data/plotovimemberi.txt"%pluginlokacija2, 'r+')
    a = filter(lambda x: x.startswith("%s:%s"%(targetplot, userID)), \
                                              textfile.readlines())
    

So current data file looks like this:

    
    
    Username:data1:data2
    Username1:data134:data453:data6534
    Username3:data5345:data678:data111:data434
    and so on...
    

What I want to achieve is:

    
    
    Username:[data1, data2]
    Username1:[data134, data453, data6534]
    Username3:[data5345, data678, data111, data434]
    and so on...
    

Why? Because I want to get certain line in file using filter I mentioned above
and edit list... So I just need to split lines using "line.split(':')[1]" and
get list of data, which I can edit using list.append and list.remove...

Thanks for reading/answering! :)


Answer: While I think I've given one answer to your question (see code below), I have
some remarks:

  * it's not 'unpythonic' to use a colon as a separator in text file; that's not Python, that's data.

  * you should look at something like YAML or JSON to store your data; you can then use libraries that take care of most of the details for you

  * with the scheme you propose, you won't be able to have a "," in your data records, nor (simply) be able to start or end records with space. These sorts of problems should be handled for you by any reasonable JSON or YAML library.

  * you could also look at something like sqlite (which comes with python) as a database store. It's a bit more work, but you'll be able to search for records based on criteria of your choosing, as well as easily delete and add records. With a plain-text file, you'll have to manage all of that yourself

Here's my solution:

    
    
    from pprint import pprint
    
    def writefile(outfilename,db):
        outfile=open(outfilename,'wt')
        for username,userdata in db:
            userdatastr= ','.join(userdata)
            outfile.write('%s: [%s]\n' % (username,userdatastr))
        # or, better, use 'with file("...") as ...'
        outfile.close()
    
    def readfile(infilename):
        infile=open(infilename,'rt')
        db=[]
        for line in infile:
            username, rest = line.split(':',1)
            lstart= rest.find('[')
            if lstart==-1:
                raise RuntimeError('couldn''t find "[" for user "%s"' % username)
            lend= rest.rfind(']')
            if lend==-1:
                raise RuntimeError('couldn''t find "]" for user "%s"' % username)
            userdata = rest[lstart+1:lend].split(',')
            db.append((username,userdata))
        return db
    
    db=[('Bob',['foo','bar','baz']),
        ('Jane',['oof','rab','zab','xuuq']),
        ('Rudy',['some','data'])]
    
    writefile('db.txt',db)
    
    indb=readfile('db.txt')
    
    print 'original:'
    pprint(db)
    print 'from file:'
    pprint(indb)
    

Result is:

    
    
     original:
     [('Bob', ['foo', 'bar', 'baz']),
      ('Jane', ['oof', 'rab', 'zab', 'xuuq']),
      ('Rudy', ['some', 'data'])]
     from file:
     [('Bob', ['foo', 'bar', 'baz']),
      ('Jane', ['oof', 'rab', 'zab', 'xuuq']),
      ('Rudy', ['some', 'data'])]
    

while db.txt looks like:

    
    
    Bob: [foo,bar,baz]
    Jane: [oof,rab,zab,xuuq]
    Rudy: [some,data]
    

