Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

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

""" 

An interface to read data files 

""" 

 

############################################################################### 

# Sage: System for Algebra and Geometry Experimentation 

# Copyright (C) 2010 Paul Zimmermann 

# Distributed under the terms of the GNU General Public License (GPL), 

# version 2 or later. The full text of the GPL is available at: 

# http://www.gnu.org/licenses/ 

############################################################################### 

 

def read_data(f,t): 

r""" 

Read data from file 'f' and class 't' (one element per line), 

and returns a list of elements. 

 

INPUT: 

 

- 'f' - a file name 

- 't' - a class (objects will be coerced to that class) 

 

OUTPUT: 

 

a list of elements of class 't'. 

 

EXAMPLES:: 

 

sage: indata = tmp_filename() 

sage: f = open(indata, "w") 

sage: _ = f.write("17\n42\n") 

sage: f.close() 

sage: l = read_data(indata, ZZ); l 

[17, 42] 

sage: f = open(indata, "w") 

sage: _ = f.write("1.234\n5.678\n") 

sage: f.close() 

sage: l = read_data(indata, RealField(17)); l 

[1.234, 5.678] 

""" 

fp = open(f,"r") 

l = [] 

while True: 

s = fp.readline() 

s = s.rstrip() 

if s == '': 

break 

l.append(t(s)) 

fp.close() 

return l