Loading a Python collection from an text-file

I

Ilias Lazaridis

within a python script, I like to create a collection which I fill with
values from an external text-file (user editable).

How is this accomplished the easiest way (if possible without the need
of libraries which are not part of the standard distribution)?

something like:

text-file:
{peter, 16},
{anton, 21}

-

within code:

users.load(text-file.txt)

for user in users
user.name
user.age

..
 
J

James Stroud

Ilias said:
within a python script, I like to create a collection which I fill with
values from an external text-file (user editable).

How is this accomplished the easiest way (if possible without the need
of libraries which are not part of the standard distribution)?

something like:

text-file:
{peter, 16},
{anton, 21}

-

within code:

users.load(text-file.txt)

for user in users
user.name
user.age

.

This is specific for the text above. You will have to re-craft a regex
if the actual file is different.

import re

def get_names(afile):
regex = re.compile(r'{([^,]*),\s*([^}]*)}')
names = []
for aline in afile:
m = regex.search(aline)
names.append(m.groups())
return names

def test():
import cStringIO
afile = cStringIO.StringIO("{peter, 16},\n{anton, 21}\n")
print get_names(afile)

test()
 
K

Ken Starks

Ilias said:
within a python script, I like to create a collection which I fill with
values from an external text-file (user editable).

How is this accomplished the easiest way (if possible without the need
of libraries which are not part of the standard distribution)?

something like:

text-file:
{peter, 16},
{anton, 21}

-

within code:

users.load(text-file.txt)

for user in users
user.name
user.age

.
"""
What I do for this kind of work is to use a gnumeric spreadsheet
which saves the data in a simple xml format. xml is much less
error-prone than plain text.
Google for, and study 'The gnumeric file format' by David Gilbert.
You need to know how to unzip the file, and how to write a SAX parser.


If you want to use a plain text format, keep it simple. I would
separate the two fields with tab (thus permit a comma within a field)
and allow 'comment' lines that start with a hash.
You don't need the braces, or the end-of-line comma you included.

# snip 'text-file.txt'
# name and age on one line separated by tab
Jonny 8
Mary 87
Moses 449


# end-snip 'text-file.txt'
Then:
"""

import string

class user:
def __init__(self,name,age):
self.name=name
self.age=int(age) # or a float, or a time-interval, or date-of-birth

def show(self):
print "%s is aged %s" % (self.name, self.age)

if __name__=="__main__":
users=[]
filename="text-file.txt"
fieldsep="\t"
F=open(filename,"r")
Lines=F.readlines()
for L0 in Lines:
L1=string.strip(L0)
if not L1.startswith("#"):
Record=string.split(L1,fieldsep)
# insert error handling/validation here
users.append(user(Record[0],Record[1]))

F.close()
for user in users:
user.show()
 
J

jschull

another approach (probably frowned upon, but it has worked for me) is
to use python syntax (a dictionary, say, or a list) and just import (or
reload) the file
 
L

Larry Bates

Take a look at ConfigParser module. The format of the file would be
something like:

[members]
peter=16
anton=21

People are accustomed to this format file (windows .ini format).

-Larry
 
I

Ilias Lazaridis

another approach (probably frowned upon, but it has worked for me) is
to use python syntax (a dictionary, say, or a list) and just import (or
reload) the file

this sounds good.

can I import a whole collection of instances this way?

-

(thanks for all the other answers within this thread).

..
 
M

Magnus Lycka

Ilias said:
within a python script, I like to create a collection which I fill with
values from an external text-file (user editable).
If a spreadsheet like layout fits, use the csv module and
a plain comma separated file.

Then the end user can also use e.g. Excel to edit the data.
 
M

Magnus Lycka

Ilias said:
this sounds good.

can I import a whole collection of instances this way?

Sure, it's just a Python module with variables in it.

I wouldn't try to teach my users Python syntax though.

If you really need this kind of data structure freedom,
I'd lean towards YAML or possibly XML. (XML isn't too
bad if you provide good tools. It's not a good idea
with just a text editor.)

If a spreadsheet like layout is enough, I (still)
recommend csv.
 
B

Bengt Richter

within a python script, I like to create a collection which I fill with
values from an external text-file (user editable).

How is this accomplished the easiest way (if possible without the need
of libraries which are not part of the standard distribution)?

something like:

text-file:
{peter, 16},
{anton, 21}

-

within code:

users.load(text-file.txt)

for user in users
user.name
user.age

.

I'd use a CSV text file, maybe something like (only tested as far as you see!):

----< for_ilias_lazaridis.py >----------------------------------------------
import csv, types

class Fields(object):
def __init__(self, kvpairs): self.__dict__.update(kvpairs)

class Users(object):
def __init__(self):
self.userlist=[]
def load(self, lineiter):
if isinstance(lineiter, basestring):
lineiter = open(lineiter) # assume it's a file path
csvit = csv.reader(lineiter)
self.colnames = colnames = csvit.next()
typenames = csvit.next()
self.coltypes =coltypes = [getattr(types, name.capitalize()+'Type')
for name in typenames]
for row in csvit:
self.userlist.append(Fields(zip(colnames, (t(s) for t,s in zip(coltypes, row)))))
def __iter__(self): return iter(self.userlist)

def test():
import StringIO
f = StringIO.StringIO("""\
name,age
String,Int
peter,16
anton,21
""")
users = Users()
users.load(f)
for user in users:
print user.name, user.age
for user in users:
for name in users.colnames:
print '%s=%s,'%(name, getattr(user, name)),
print

if __name__ == '__main__': test()
-----------------------------------------------------------------------

Output:

[ 4:47] C:\pywk\clp>py24 for_ilias_lazaridis.py
peter 16
anton 21
name=peter, age=16,
name=anton, age=21,

(the first for user in users loop presumes knowledge of the field names name and age.
The second gets them automatically from the names loaded in the load method from
the first line of the text file. The second line expects type names as you see
in the types module, except without the "Type" suffix.

Perhaps you can adapt for your purposes.

Regards,
Bengt Richter
 
I

Ido Yehieli

Sure, it's just a Python module with variables in it.
not to mention the security risks
 
I

Ilias Lazaridis

[...]

the solutions below seems to be the most compact one.

this, or the suggested CSV module within the other messages.

thank's to everyone for the feedback.

[...]
If you want to use a plain text format, keep it simple. I would
separate the two fields with tab (thus permit a comma within a field)
and allow 'comment' lines that start with a hash.
You don't need the braces, or the end-of-line comma you included.

# snip 'text-file.txt'
# name and age on one line separated by tab
Jonny 8
Mary 87
Moses 449


# end-snip 'text-file.txt'
Then:
"""

import string

class user:
def __init__(self,name,age):
self.name=name
self.age=int(age) # or a float, or a time-interval, or date-of-birth

def show(self):
print "%s is aged %s" % (self.name, self.age)

if __name__=="__main__":
users=[]
filename="text-file.txt"
fieldsep="\t"
F=open(filename,"r")
Lines=F.readlines()
for L0 in Lines:
L1=string.strip(L0)
if not L1.startswith("#"):
Record=string.split(L1,fieldsep)
# insert error handling/validation here
users.append(user(Record[0],Record[1]))

F.close()
for user in users:
user.show()

..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top