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

Shannon -jj Behrens jjinux at gmail.com
Wed Apr 6 00:47:52 CEST 2011


Perhaps something like:

f = open("data.txt")
matrix = [map(int, line.split()) for line in f]

-jj

On Tue, Apr 5, 2011 at 3:42 PM, Alexandre Conrad
<alexandre.conrad at gmail.com> wrote:
> Is that what you want?
>
>>>> with open("data.txt") as f:
> ...     data = f.read().split()
> ...
>>>> data
> ['19', '19', '20', '21', '22', '17', '18', '19', '20', '21', '22',
> '23', '41', '42', '17', '18', '19', '20']
>
> ??
>
> 2011/4/5 Yiou Li <liyiou at gmail.com>:
>> 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
>> _______________________________________________
>> Baypiggies mailing list
>> Baypiggies at python.org
>> To change your subscription options or unsubscribe:
>> http://mail.python.org/mailman/listinfo/baypiggies
>>
>
>
>
> --
> Alex | twitter.com/alexconrad
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies



-- 
In this life we cannot do great things. We can only do small things
with great love. -- Mother Teresa
http://jjinux.blogspot.com/


More information about the Baypiggies mailing list