Reading data from file

1,025 views
Skip to first unread message

H.S.Rai

unread,
Dec 28, 2008, 8:36:26 PM12/28/08
to sage-s...@googlegroups.com
What is the easiest way to read data from file. file function mention
of size, I want to read variable wise, like:

f.read(myInteger1, myFloat1, myString1)

My data file will be:
'b' 'breadth of member' 250
'D' 'overall depth of member' 550

Each line having three entries: <variable name>,<Description>,<Default vaule>

These may be separated by comma, tab or space. If I am able to read
and have list like:

varList[ [ 'b', 'breadth of member', 250 ] , [ 'D', 'overall depth of
member', 550 ] ]

then that will serve my purpose.

With regards,

--
H.S.Rai

William Stein

unread,
Dec 28, 2008, 9:02:52 PM12/28/08
to sage-s...@googlegroups.com

Please send another email with a short example file attached, which is
*exactly* like the file you want to read in. I'm sure somebody will
respond with a snippet of code to do what you want.

The code will likely look like

varList = [x.replace(',',' ').split() for x in open('file').readlines()]

William

David Joyner

unread,
Dec 28, 2008, 9:03:05 PM12/28/08
to sage-s...@googlegroups.com
One way:


sage: f = open("mydatafile.txt")
sage: l1 = f.readline()
sage: ls1 = l1.split(" ")
sage: l1; ls1
"'b' 'breadth of member' 250\n"
["'b'", "'breadth of member'", '250\n']
sage: eval(ls1[2])
250


where mydatafile.txt is in $SAGEROOT and consists of these lines:

'b' 'breadth of member' 250
'D' 'overall depth of member' 550

and each item is separated by 2 spaces.

H.S.Rai

unread,
Dec 30, 2008, 8:07:03 PM12/30/08
to sage-s...@googlegroups.com
On Mon, Dec 29, 2008 at 7:33 AM, David Joyner <wdjo...@gmail.com> wrote:
>
> sage: f = open("mydatafile.txt")
> sage: l1 = f.readline()

Thanks to William and David for helping me. I may able to achieved
what I targeted with following code:

f = open('example01Data.txt')
varList = [x.split(' // ') for x in f.readlines()]

where data in example01Data.txt is separate by " // ", sample data is
below signature.

Now the problem I am facing to use it in LaTeX, is reported in next email.

Thanks and regards,

--
H.S.Rai
=======================

b // breadth of beam // 300
d // effective depth of beam // 550
D // overall depth of beam // 600
A_st // area of reinforcement // 0
phi_b // diameter of reinforcing steel bar // 12

Reply all
Reply to author
Forward
0 new messages