0

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! :)

Amar Kalabić
  • 888
  • 4
  • 15
  • 33

5 Answers5

1

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]
Rory Yorke
  • 2,166
  • 13
  • 13
0

I think you should read about ConfigParser https://docs.python.org/2/library/configparser.html. This library can solve your problem with reading and writing. You can read here more about using lists in ConfigParser -> Lists in ConfigParser

Community
  • 1
  • 1
Mariusz
  • 349
  • 2
  • 7
0

You could use pickle or pyyaml to store your objects in text files. Example of using pickle:

import pickle
# save some list
lst=['hello','pickle']
pickle.dump(lst, open('lst.txt','w'))
# load list from txt file
lst2=pickle.load(open('lst.txt'))
print(lst2)

If you want to store data in readable form you could use pyyaml:

from yaml import load, dump
# dump list in readable form
lst=['hello', 'yaml']
open('lst2.txt', 'w').write(dump(lst))
# load list from yaml file
print(load(open('lst2.txt')))
Aleksei Shestakov
  • 2,508
  • 2
  • 13
  • 14
  • Pickles look nice but this will return me everything from that file right? I need to get only certain lines in that file like I did using "filter" I put above... Read bottom of question above, I gave explanation on how data is stored and how I want to store it... – Amar Kalabić May 04 '14 at 17:48
0

Edited accordingly:

list = [data1, data2, data3, data++]

file = open("file.txt", "w")
file.write("Username: " + str(list))
file.close
Beta Decay
  • 805
  • 1
  • 8
  • 20
0

I think you need a dictionary, not a list, unless the Username is not unique, where you could use a list.

data_text = '''Username1:data1:data2:data3:data++
Username2:data1:data2:data3:data++
Username3:data1:data2:data3:data++
Username3:data4:data5:data6:data++'''

lines = data_text.split('\n')

data_dict = {}
data_list = []
for l in lines:
    fields = l.split(':')
    user = fields[0]
    data = fields[1:]
    data_dict[user] = data
    data_list.append((user,data))

print(data_text)
print(data_dict)
print(data_list)
chapelo
  • 2,519
  • 13
  • 19