'xlrd'에 해당되는 글 2건

  1. 2007.08.25 xlrd 연습
  2. 2007.08.25 xlrd 주요 내용

xlrd 연습

project/python 2007. 8. 25. 18:41

주어진 엑셀 파일에서 종목코드 및 특정 셀의 값을 읽어서 매개변수로 사용하는 방법

# coding: utf-8
# *-* coding: utf-8 -*-
# -- coding cp949 --
import xlrd, time
book = xlrd.open_workbook("C:\Python25\getting_started\myfile.xls")
print "The number of worksheets is", book.nsheets   #sheet의 갯수

print "Worksheet name(s):", book.sheet_names()

sh = book.sheet_by_index(0)
#sh = book.sheet_by_name("test")    #index로만 하면 sheet의 이름을 마음대로 쓰지 못하니까 이것으로 하는 것이 더 좋다
#print sh.cell_type(0, 0)
#print sh.cell_type(0, 1)
#print sh.cell_type(0, 2)
#print sh.cell_type(1, 0)
#print sh.cell_type(1, 1)
#print sh.cell_type(1, 2)
#print sh.cell_type(2, 0)
#print sh.cell_type(2, 1)
#print sh.cell_type(2, 2)

print "_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/"

#print sh.cell_value(0, 0)
#print sh.cell_value(0, 1)
#print sh.cell_value(0, 2)
#print sh.cell_value(1, 0)
#print sh.cell_value(1, 1)
#print sh.cell_value(1, 2)
#print sh.cell_value(2, 0)
#print sh.cell_value(2, 1)
#print sh.cell_value(2, 2)

print "_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/"

#print sh.col(0)
print "_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/"

#print sh.col_slice(0, start_rowx=1, end_rowx=100)

#print sh.name, sh.nrows, sh.ncols
#print sh.row(2)
#print sh.col(0)
startcol = 7
endcol = 10
print sh.col_slice(0, startcol, endcol)

sh2 = book.sheet_by_index(1)
start = int(sh2.cell_value(0, 0))   #읽어오는 셀의 값에 int()를 씌워버렸더니 그냥 integer로 인식
end = int(sh2.cell_value(1, 0))   #읽어오는 셀의 값에 int()를 씌워버렸더니 그냥 integer로 인식
#print start
#print end
print sh.col_slice(0, start, end)

#print sh.row_types(2)
#print sh.row_values(2)
print "Cell D30 is", sh.cell_value(rowx=29, colx=3)
t = time.time()

#for rx in range(sh.nrows):
#    print sh.row(rx)
#for rx in range(sh.ncols):
#    print sh.col(rx)
print 'Python Elapsed %.02f' % (time.time() - t)

Posted by trigger
,

xlrd 주요 내용

project/python 2007. 8. 25. 17:06
Book Class

nsheets: worksheet의 갯수

sheet_by_index(sheetx)

       sheetx: sheet1의 경우에는 0, sheet2의 경우는 1. 이런 식으로 사용
       Returns: 특정 sheet를 선택

sheet_by_name(sheet_name)
       sheet_name: "시트네임" 이런 식으로 사용
       Returns: 특정 sheet를 선택

sheet_names()
       Returns: 시트의 이름들

Sheet Class

Sheet: sheet속의 모든 데이타. 그러나 이 클래스를 직접 불러서 쓸 일은 없고, xlrd.open_workbook("myfile.xls")와 같이 workbook을 읽어와서 sheet_by_index나 sheet_by_name으로 선택한다

Cell(ctype, value): type과 value로 이루어져 있고, 직접 이 클래스를 불러서 쓰진 않는다

cell_type(rowx, colx): cell_type(3, 4) 이런 식으로 쓰며, 셀에 있는 데이타의 type을 반환

cell_value(rowx, colx): cell_type(3, 4) 이런 식으로 쓰며, 셀에 있는 데이타의 value를 반환

col(colx): 예를 들어 보면, 한 칼럼에 있는 전체 내용을 반환한다.

[text:u'Symbol', text:u'A057330', text:u'A038500', text:u'A004870', text:u'A032580', text:u'A047600', text:u'A036420', text:u'A053070', text:u'A017170', text:u'A051530', text:u'A032820', text:u'A053170', text:u'A058550', text:u'A041940' 이런 식으로

row(rowx): 예를 들어 보면, 한 행에 있는 전체 내용을 반환한다.

col_slice(colx, start_rowx=0, end_rowx=None): start_rowx와 end_rowx를 직접 써 넣어서 특정칼럼의 특정부분만 잘라낸다(slice). end_rowx에서 숫자를 안 쓰고 None으로 해도 작동한다. 이 경우에는 내용이 있는 cell까지 계속해서 데이타를 읽어 들인다
row_slice(rowx, start_colx=0, end_colx=None): 위와 같고 행이라는 것만 차이점

name: sheet의 이름

ncols: number of cols(빈 칸이 아닌 내용이 있는 칼럼)
nrows: number of rows(빈 칸이 아닌 내용이 있는 행)

row_types(rowx): 특정 row의 type(cell type과 같은 형태)
row_values(rowx): 특정 row의 값

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

row_slice(rowx, start_colx=0, end_colx=None)를 이용하면 종목코드를 원하는 부분만 읽어올 수 있다
Posted by trigger
,