1. CSV file I/O

Reference

ImageJ + Python Cookbook

1. Loading CSV File

  • csv 파일을 읽는 예제입니다.

  • 예제 파일은 여기에서 다운받을 수 있습니다.

1.1. CSV 파일 읽어서 결과 보여주기

1
2
3
4
5
6
7
import csv

filepath = r'C:\Tmp\loadexample.csv'
f = open(filepath, 'rb')
data = csv.reader(f, delimiter=' ')
for row in data:
print ', '.join(row)
  • 실행결과


1.2. CSV 파일 읽어서 특정 컬럼을 창으로 띄워주기

1
2
3
4
5
6
7
8
9
10
11
12
from ij import IJ
from util.opencsv import CSVReader
from java.io import FileReader

def readCSV(filepath):
reader = CSVReader(FileReader(filepath), ",")
ls = reader.readAll()
for item in ls:
IJ.log(item[2])

filepath = r'C:\Tmp\loadexample.csv'
readCSV(filepath)
  • 실행결과


2. Save as CSV File

  • csv 파일을 쓰는 예제입니다.

2.1. python방식 : pythonic

1
2
3
4
5
6
7
8
9
import csv

f = open(r'C:\Tmp\saveexample1.csv', 'wb')
writer = csv.writer(f)
writer.writerow(['does this work'])
writer.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
#can chop down more
writer.writerows(['Spam', 'Lovely Spam', 'Wonderful Spam'])
f.close()
  • 실행결과


2.2. Numerical data (pythonic)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os, csv

# prepare test data to write to a csv file
data1 = range(10)
data2 = [x * x for x in data1]
data3 = [pow(x, 3) for x in data1]
print data3

# prepare path
root = r"C:\Tmp"
filename = r"saveexample2.csv"
fullpath = os.path.join(root, filename)
print fullpath

# open the file first (if its not there, newly created)
f = open(fullpath, 'wb')

# create csv writer
writer = csv.writer(f)

# for loop to write each row
for i in range(len(data1)):
row = [data1[i], data2[i], data3[i]]
writer.writerow(row)

#writer.writerows([data1, data2, data3])

# close the file.
f.close()
  • 실행결과


2.3. java 활용

1
2
3
4
5
6
7
8
9
10
11
12
13
from util.opencsv import CSVWriter
from java.io import FileWriter
from java.lang.reflect import Array
from java.lang import String, Class

writer = CSVWriter(FileWriter(r'C:\Tmp\saveexample3.csv'), ',')
data = Array.newInstance(Class.forName("java.lang.String"), 3)

data[0] = str(11)
data[1] = str(23)
data[2] = str(5555)
writer.writeNext(data)
writer.close()
  • 실행결과


2.4. javajarray module 활용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from util.opencsv import CSVWriter
from java.io import FileWriter
from java.lang import String
from jarray import array as jarr

writer = CSVWriter(FileWriter(r'C:\Tmp\saveexample4.csv'), ',')
header = ['x', 'y', 'z']
jheader = jarr(header, String)
data = [11,23,5555]
datas = map(str, data)
jdata = jarr(datas, String)
writer.writeNext(jheader)
writer.writeNext(jdata)
writer.close()
  • 실행결과



도움이 되셨나요?

카페인을 투입하시면 다음 포스팅으로 변환됩니다

Share