LoginSignup
1
1

More than 1 year has passed since last update.

python標準ライブラリだけを使って、タブ区切りの数値のテキストファイルをリストに入れる

Posted at

pandaなどのモジュールを使うのが一般的だが、そのようなものが使えない環境もある。
ここでは、pythonの標準ライブラリだけをつかって行う方法を述べる。

input.txt
100	400	0	60
100	400	100	60
300	400	500	60
100	500	500	60
400	-500	-200	40
...

以下のようにリスト内包表記で書けばいい。
なお、ここでは、リストのリストとして扱っている。

with open('input.txt','r') as f:
    fl = f.read().split('\n')

input = [fl[i].split('\t') for i in range(len(fl))]
input = [[ int(input[i][j]) for j in range(len(input[i]))] for i in range(len(fl))]

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1