Python read text file columnwise

Peter Otten __peter__ at web.de
Sat Jan 12 04:19:24 EST 2019


shibashibani at gmail.com wrote:

> Hello
>> 
>> I'm very new in python. I have a file in the format:
>> 
>> 2018-05-31	16:00:00	28.90	81.77	4.3
>> 2018-05-31	20:32:00	28.17	84.89	4.1
>> 2018-06-20	04:09:00	27.36	88.01	4.8
>> 2018-06-20	04:15:00	27.31	87.09	4.7
>> 2018-06-28	04.07:00	27.87	84.91	5.0
>> 2018-06-29	00.42:00	32.20	104.61	4.8
> 
> I would like to read this file in python column-wise.
> 
> I tried this way but not working ....
>   event_list = open('seismicity_R023E.txt',"r")
>     info_event = read(event_list,'%s %s %f %f %f %f\n');

There is actually a library that implements a C-like scanf. You can install 
it with

$ pip install scanf

After that:

$ cat read_table.py
from scanf import scanf

with open("seismicity_R023E.txt") as f:
    for line in f:
        print(
            scanf("%s %s %f %f %f\n", line)
        )
$ cat seismicity_R023E.txt 
2018-05-31      16:00:00        28.90   81.77   4.3
2018-05-31      20:32:00        28.17   84.89   4.1
2018-06-20      04:09:00        27.36   88.01   4.8
2018-06-20      04:15:00        27.31   87.09   4.7
2018-06-28      04.07:00        27.87   84.91   5.0
2018-06-29      00.42:00        32.20   104.61  4.8
$ python read_table.py 
('2018-05-31', '16:00:00', 28.9, 81.77, 4.3)
('2018-05-31', '20:32:00', 28.17, 84.89, 4.1)
('2018-06-20', '04:09:00', 27.36, 88.01, 4.8)
('2018-06-20', '04:15:00', 27.31, 87.09, 4.7)
('2018-06-28', '04.07:00', 27.87, 84.91, 5.0)
('2018-06-29', '00.42:00', 32.2, 104.61, 4.8)
$

However, in the long term you may be better off with a tool like pandas:

>>> import pandas
>>> pandas.read_table(
... "seismicity_R023E.txt", sep=r"\s+",
... names=["date", "time", "foo", "bar", "baz"],
... parse_dates=[["date", "time"]]
... )
            date_time    foo     bar  baz
0 2018-05-31 16:00:00  28.90   81.77  4.3
1 2018-05-31 20:32:00  28.17   84.89  4.1
2 2018-06-20 04:09:00  27.36   88.01  4.8
3 2018-06-20 04:15:00  27.31   87.09  4.7
4 2018-06-28 04:00:00  27.87   84.91  5.0
5 2018-06-29 00:00:00  32.20  104.61  4.8

[6 rows x 4 columns]
>>>

It will be harder in the beginning, but if you work with tabular data 
regularly it will pay off.




More information about the Python-list mailing list