0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonでデータをテキストファイルに入出力するとき

Last updated at Posted at 2022-05-03

はじめに

pythonで外部のデータを読み込むとか外部に出力するとかの書き方が一生覚えられず,毎回調べているので,自分用にまとめる.

txtファイルから情報を読み込むとき

file = open('file.txt', 'r', encoding='UTF-8')

for f in file:
  print(f.rstrip('\n'))

file.close()

numpyで読み込み

data = np.loadtxt('file.txt')

numpyでデータを保存するとき

gnuplotなどでグラフを書くときにデータをテキストファイルで出力する必要があるので,numpyでのやり方を記す.

data = np.array([1,2])
np.savetxt('data.txt', data)

ついでにリストからnumpyに変換する方法も必要だと思うので書いておく.

list = [1,2,3]
np_list = np.array(list)

numpyの転置は次の通り.

list = [[1,2]
       ,[3,4]]
list_T = list.T

>list_T
>[[1,3]
  [2,4]]

以降,またなにか必要になったら追記する.

0
3
0

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
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?