reading file to list

MRAB google at mrabarnett.plus.com
Sat Jan 17 12:44:12 EST 2009


Tino Wildenhain wrote:
> Xah Lee wrote:
>> comp.lang.lisp,comp.lang.scheme,comp.lang.functional,comp.lang.python,comp.lang.ruby 
>>
>>
> ...
>> OK, I want to create a nested list in Lisp (always of only integers)
>> from a text file, such that each line in the text file would be
>> represented as a sublist in the 'imported' list.
>>
>> example of a file's content
>>
>> 3 10 2
>> 4 1
>> 11 18
>>
>> example of programing behavior
>> (make-list-from-text "blob.txt") => ((3 10 2) (4 1) (11 18))
> 
>>
>> In a show-off context, it can be reduced to by about 50%, but still
>> far verbose than ruby or say perl (which is 1 or 2 lines. (python
>> would be 3 or 5)).
> 
> So? Please count the lines:
> 
> [line.strip().split() for line in file("blob.txt")]
> 
The original requirement was for a list of lists of integers:

[[int(x) for x in line.split()] for line in open("blob.txt")]

Still only 1 line, though.



More information about the Python-list mailing list