[Tutor] Importing file data into Python arrays

Yigal Duppen yduppen@xs4all.nl
Mon, 27 May 2002 23:24:06 +0200


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> 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
> arr=[]
> inp = open ("dat.txt","r")
> #read line into array
> for line in inp.readlines():
> 	arr.append(line)
> 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"
>>> line
'1 2 3\n'

First, the string needs to be splitted into its number parts. This is done 
using the split function.

>>> line.split()
['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"
>>> commaline
'1,2,3\n'
>>> commaline.strip()
'1,2,3'
>>> commaline.strip().split(",")
['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:

arr=[]
inp = open ("dat.txt","r")
#read line into array
for line in inp.readlines():
	numbers = map(int, line.split())
	arr.append(numbers)
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

import string
...
numbers = map(int, string.split(line))


YDD
- -- 
.sigmentation Fault
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE88qP2LsKMuCf5EdwRAli6AKDzDOmem1ANhN2KiXME6YDHAEHdfwCgrpk9
bNhzoN9Mgl9Q9daVNNf9izM=
=REan
-----END PGP SIGNATURE-----