[Tutor] Importing file data into Python arrays
Mon, 27 May 2002 23:24:06 +0200
-----BEGIN PGP SIGNED MESSAGE-----
> However, when I try to read in data from a file as shown below, I end
> up with the lines, which I can't manipulate.
> import array
> inp = open ("dat.txt","r")
> #read line into array
> for line in inp.readlines():
> print arr [0:3]
> which gives
> ['1 2 3\n', '4 5 6\n', '7 8 9']
> as output
In the following I'll just assume you want the output
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Readline returns strings (such as '1 2 3\n'). Luckily, it is easy to convert
this string to a list of numbers.
>>> line = "1 2 3\n"
'1 2 3\n'
First, the string needs to be splitted into its number parts. This is done
using the split function.
['1', '2', '3']
The split function can also takes other arguments, which is great if you have
for example a comma separated input. Of course, in that case you need to to
trim the trailing whitespace (newline)
>>> commaline = "1,2,3\n"
['1', '2', '3']
But I digress. In any case, you still have a number of strings. That's where
the map function comes in handy.
>>> map(int, line.split())
[1, 2, 3]
This applies the int() function on every element in your list, resulting in a
new list containing the converted values.
So your total program would look like:
inp = open ("dat.txt","r")
#read line into array
for line in inp.readlines():
numbers = map(int, line.split())
print arr [0:3]
Note: this only works with Python 2.0 or higher. For Python 1.5 you need to
import the string module
numbers = map(int, string.split(line))
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org
-----END PGP SIGNATURE-----