[Baypiggies] Question about python read data table from a text file

Yiou Li liyiou at gmail.com
Wed Apr 6 01:07:50 CEST 2011


Thank you all. The split() thing works!

Leo


On Tue, Apr 5, 2011 at 4:03 PM, Patrick Thomas <psthomas at gmail.com> wrote:
> Try this:
>
> data = [line.strip().split() for line in open("data.txt")]
>
> print data[0][0] #first row, first col
> print data[1][0] #second row, first col
>
> ~PST
>
> On Tue, Apr 5, 2011 at 3:32 PM, Yiou Li <liyiou at gmail.com> wrote:
>> Hi all,
>>
>> I have a text file with numerical data, like this:
>>
>> data.txt
>>
>> 19  19  20  21  22  17  18  19  20  21  22  23  41  42  17  18  19  20
>>  21  42  45  18  19  20  21  22  20  21  22  23  42  43  17  19  20
>> 21  22  23  24  40  41  42  43  44  45  46  47  16  17  18  19  20  21
>>  22  41  42  43  44  45  46  47  16  17  18  19  20  45  46  47  17
>> 18  19  20  41  42  43  44  45  20  21  22  23  40  41  42  43  44  45
>>  46  47  48  20  21  22  23  24  40  41  42  43  44  45  46  47  48
>> 17  18  19  ...
>>
>> Could you please refer me a python function to read this file into the
>> memory as a data table?
>>
>> the readlines() won't work because I need to access index the number
>> of ith row, jth column like x[i, j].
>>
>> Best,
>> Leo
>>
>


More information about the Baypiggies mailing list