471,873 Members | 949 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,873 software developers and data experts.

Need Help Parsing From File

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?
Dec 7 '06 #1
12 1261
John Frame wrote:
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')]

;)
--
Soni Bergraj
http://www.YouJoy.org/
Dec 7 '06 #2
At Thursday 7/12/2006 00:20, John Frame wrote:
>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
Dec 7 '06 #3

Gabriel Genellina wrote:
>
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.
>>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?

Cheers,
John

Dec 7 '06 #4
At Thursday 7/12/2006 02:51, John Machin wrote:
>Gabriel Genellina wrote:

ftxt=open(filename,"rt")

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", 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
Dec 7 '06 #5

Gabriel Genellina wrote:
At Thursday 7/12/2006 02:51, John Machin wrote:
Gabriel Genellina wrote:
>
ftxt=open(filename,"rt")
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

Dec 7 '06 #6
At Thursday 7/12/2006 15:44, John Machin wrote:
ftxt=open(filename,"rt")
>
>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.
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
Dec 8 '06 #7
On 8/12/2006 1:53 PM, Gabriel Genellina wrote:
At Thursday 7/12/2006 15:44, John Machin wrote:
ftxt=open(filename,"rt")

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.

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.
>(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.
So it should be ignored.

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

Dec 8 '06 #8
John Machin wrote:
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>

Dec 8 '06 #9
At Friday 8/12/2006 02:23, John Machin wrote:
> ftxt=open(filename,"rt")

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).
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.
>>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.

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
Dec 8 '06 #10
Gabriel Genellina <ga******@yahoo.com.arwrote:
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 Machin wrote:
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.
Dec 8 '06 #11
At Friday 8/12/2006 05:30, Duncan Booth wrote:
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'.
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
Dec 8 '06 #12
On 2006-12-07 04:20:48 +0100, John Frame <jf****@vt.edusaid:
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

Dec 10 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Doug R | last post: by
1 post views Thread by sommarlov | last post: by
4 posts views Thread by Rick Walsh | last post: by
2 posts views Thread by Anders B | last post: by
25 posts views Thread by Jon Slaughter | last post: by
6 posts views Thread by Damien87 | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.