Need Help Parsing From File

J

John Frame

Hi, I've got a Python program that I'm trying to edit, and I need some
help.

If I would like to read a matrix from a previously created text file
into a two dimensional array, how would I do that?

Like, if in the txt file, I had the following matrix formatted numbers
with 5 rows and 10 columns, and each number is separated by a single space:

11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60

How would I read this data from the file into a two dimensional array in
Python?
 
S

Soni Bergraj

John said:
How would I read this data from the file into a two dimensional array in
Python?

Like:
[x.split() for x in open('myfile.txt')]

Or if you need integers:
[[int(a) for a in x.split()] for x in open('myfile.txt')]

;)
 
G

Gabriel Genellina

Like, if in the txt file, I had the following matrix formatted numbers
with 5 rows and 10 columns, and each number is separated by a single space:

11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60

How would I read this data from the file into a two dimensional array in
Python?

If "two dimensional array" means "a list of 5 elements each one being
a list of 10 integers" you can do this:

ftxt=open(filename,"rt")
arr = []
for line in ftxt:
arr.append([int(number) for number in line.split()])
ftxt.close()

or, not so legible but more compact:

ftxt=open(filename,"rt")
arr2 = [[int(number) for number in line.split()] for line in ftxt]
ftxt.close()

or using Python 2.5:

with open(filename,"rt") as ftxt:
arr3 = [[int(number) for number in line.split()] for line in ftxt]


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 
J

John Machin

Gabriel said:
ftxt=open(filename,"rt")

Never seen that done before. It's not in the docs.

FWIW:

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.# Nary an exception raised, not the slightest murmur

Is this a bug or a feature? Or is it one of those good old "unspecified
behaviour" cases? MSVC rtl only?

Cheers,
John
 
G

Gabriel Genellina

Never seen that done before. It's not in the docs.

A remnant of my MSDOS+C background...
FWIW:

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.# Nary an exception raised, not the slightest murmur

Is this a bug or a feature? Or is it one of those good old "unspecified
behaviour" cases? MSVC rtl only?

The Python docs say only that the initial letter is checked. And the
ANSI 89 C says that other characters may follow after r, r+, etc.
"rt" is useless for an ANSI C compiler, since the default stream mode
is "text" -on systems which differentiate between text and binary-
and irrelevant on systems which don't do such distinction.
(And since I got used to write "rt", you can infer something about
*when* I began to write C programs...)


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 
J

John Machin

Gabriel said:
Never seen that done before. It's not in the docs.

A remnant of my MSDOS+C background...
FWIW:

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
f = open('foo.txt', 'rt')
f = open('foo.txt', 'rs')
f = open('foo.txt', 'ratsdroppings')
# Nary an exception raised, not the slightest murmur

Is this a bug or a feature? Or is it one of those good old "unspecified
behaviour" cases? MSVC rtl only?

The Python docs say only that the initial letter is checked. And the
ANSI 89 C says that other characters may follow after r, r+, etc.
"rt" is useless for an ANSI C compiler, since the default stream mode
is "text" -on systems which differentiate between text and binary-
and irrelevant on systems which don't do such distinction.
(And since I got used to write "rt",

Why did you do that?
(1) Text mode is was and ever shall be the default, even with MS.
(2) Seeing we're referring to docs and standards: Microsoft C 5.0
Optimizing Compiler, Run-Time Library Reference manual says "The t
option is not part of the ANSI standard for open, but is a Microsoft
extension and should not be used where ANSI portability is required".
you can infer something about
*when* I began to write C programs...)

Youngster :)

Cheers,
John
 
G

Gabriel Genellina

Why did you do that?
(1) Text mode is was and ever shall be the default, even with MS.

No, there used to be a flag in the stdio library, giving the default
value when neither t or b was specified.
For the Borland C++ 3.1 help (about 1991):
If "t" or "b" is not given in the string, the mode is governed by _fmode.
If _fmode is set to O_BINARY, files are opened in binary mode.
If _fmode is set to O_TEXT, they are opened in text mode.
MSC used to have a similar flag (perhaps using the same name).
All of this predates wide usage of ANSI C 89, which made the "t" flag obsolete.
(2) Seeing we're referring to docs and standards: Microsoft C 5.0
Optimizing Compiler, Run-Time Library Reference manual says "The t
option is not part of the ANSI standard for open, but is a Microsoft
extension and should not be used where ANSI portability is required".

A "t" after the initial recognized characters should be ignored by
any conforming compiler. I think the idea was to allow for extensions
like fopen(fn, "rt;reclen=128") but except esoteric platforms I doubt
anyone is using that.


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 
J

John Machin

No, there used to be a flag in the stdio library, giving the default
value when neither t or b was specified.
For the Borland C++ 3.1 help (about 1991):
If "t" or "b" is not given in the string, the mode is governed by _fmode.
If _fmode is set to O_BINARY, files are opened in binary mode.
If _fmode is set to O_TEXT, they are opened in text mode.
MSC used to have a similar flag (perhaps using the same name).

Indeed, and their docs say that the default for _fmode is text mode.
All of this predates wide usage of ANSI C 89, which made the "t" flag
obsolete.


A "t" after the initial recognized characters should be ignored by any
conforming compiler. I think the idea was to allow for extensions like
fopen(fn, "rt;reclen=128") but except esoteric platforms I doubt anyone
is using that.

So it should be ignored.

So, back to the question: why did you get into the habit of writing "rt"
when it was pointless?
 
F

Fredrik Lundh

John said:
Indeed, and their docs say that the default for _fmode is text mode.

the default doesn't matter much if you're writing library code for an
application that may change it.

</F>
 
G

Gabriel Genellina

Indeed, and their docs say that the default for _fmode is text mode.

But anyone could change that *global* flag, making "binary" the
default afterwards. So if you really wanted a text file you had to be explicit.
So it should be ignored.
So, back to the question: why did you get into the habit of writing
"rt" when it was pointless?

Because it was *not* pointless before ANSI C.


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 
D

Duncan Booth

Gabriel Genellina said:
For the Borland C++ 3.1 help (about 1991):
If "t" or "b" is not given in the string, the mode is governed by
_fmode.
If _fmode is set to O_BINARY, files are opened in binary mode.
If _fmode is set to O_TEXT, they are opened in text mode.
MSC used to have a similar flag (perhaps using the same name).

I assume you are using 'used to have' in the sense of 'still have, although
it is now deprecated in favour of the functions _get_fmode and _set_fmode'.

John said:
Indeed, and their docs say that the default for _fmode is text mode.

Quite apart from the scope for the program to change the value at runtime,
the default may also be changed to binary mode by linking the program with
binmode.obj.
 
G

Gabriel Genellina

I assume you are using 'used to have' in the sense of 'still have, although
it is now deprecated in favour of the functions _get_fmode and _set_fmode'.

I meant "I have no idea whether it's still the case or not" - thanks
for the info. And I was talking about the old MSC compiler, a quite
different product from the current MSVC.


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 
M

Markus Rosenstihl

Hi, I've got a Python program that I'm trying to edit, and I need some help.

If I would like to read a matrix from a previously created text file
into a two dimensional array, how would I do that?

Like, if in the txt file, I had the following matrix formatted numbers
with 5 rows and 10 columns, and each number is separated by a single
space:

11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60

How would I read this data from the file into a two dimensional array
in Python?

In case you have imported pylab:

myarray = pylab.load('textfile.txt')

Regards
Markus
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top