LoginSignup
6
10

More than 5 years have passed since last update.

Pythonでcsv

Last updated at Posted at 2017-08-09

Pythonでcsvファイルからデータを読み込む

(python3)

import csv
csvfile = open("filename", 'rt')
contents = csv.reader(csvfile, delimeter=',')
    #delimiterの値を区切り文字として、読み込む。
for row in contents:
    """
    contentsをそのまま出力してもファイルの中身が
    出力されるわけではないので、各行を取り出している。
    """
    print(row)

例えば、

filenameの中身が

,A,B,C,D,E
1,234,513,25345,5243,52
2,513,52345,523452,5234,45
3,31,5432,54,63,63

だとすると、
以下のようにコードを書けば、

import csv
import numpy as np
csvfile = open("filename", 'rt')
contents = csv.reader(csvfile, delimiter=',')
array = np.array( [ row for row in contents ] )
array = array[1:, 1:]
print(array)

次のような出力結果が得られる。

[['234' '513' '25345' '5243' '52']
 ['513' '52345' '523452' '5234' '45']
 ['31' '5432' '54' '63' '63']]
6
10
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
6
10