Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
9
Help us understand the problem. What is going on with this article?
@shima_x

xlrdでpythonからexcelファイルを操作する(個人的メモ)

More than 5 years have passed since last update.

参考URL

参考コード

# coding: utf-8

from xlrd import open_workbook, XL_CELL_TEXT , cellname
import re
from datetime import datetime

wtdlist = ('月', '火', '水' , '木', '金', '土', '日' )

wb = open_workbook('test.xls')
outfile = open('bscs_rate', 'w', 1000 )

for s in wb.sheets():
    print 'Sheet:',s .name
    channel = s .cell(0 ,0 ).value
    # date -> 2014-02-01
    ymd = s .cell(0 ,26 ).value
    s_date, others = ymd.strip().split( '(')
    tdatetime = datetime.strptime(s_date, '%Y年%m月%d日')

    # get week day
    wtd = wtdlist[tdatetime.weekday()]

    # get tuned date
    ymd = tdatetime.strftime( '%Y-%m-%d')

    for row in xrange(4,s .nrows):
        if s.cell(row,1).value==u'': continue

        start_time = ''
        minute = ''
        region = ''
        title = ''
        rate = ''

        values = []
        start_time = unicode( int( s.cell(row,1).value) )
        start_time = start_time if len(start_time)== 4 else '0'+start_time
        start_time = start_time[: 2]+ ':'+start_time[2 :]
        minute = unicode( int( s.cell(row,2).value) )
        title = s.cell(row,3).value

        for col in xrange( 6, 9):
            region = s.cell(2,col).value
            region = re.sub(ur '\n','' ,region).strip()
            region = u '関西' if region==u'近畿' else region
            region = u '名古屋' if region==u'中部' else region

            rate = unicode( s.cell(row,col).value)

            values = [region, ymd, wtd, title, channel, start_time, minute, rate]
            str = u','.join(values)
            print str
            outfile.write(str+ '\n')
9
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
shima_x
分析とか雑用とかやってます

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
9
Help us understand the problem. What is going on with this article?