Created
January 16, 2014 01:02
-
-
Save dylan4224/8447942 to your computer and use it in GitHub Desktop.
使用xlrd,xlwt,进行读写Excel文件,测试中文是否乱。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
#Excel读写测试 | |
#使用xlrd,xlwt,进行读写Excel文件,测试中文是否乱码 | |
#修订历史: | |
# 2014-1-15 [email protected] init | |
def read_excel(excel): | |
"""Read data from excel, which is supposed to be an excel.""" | |
from xlrd import open_workbook | |
wb = open_workbook(excel) | |
datum = [] | |
s = wb.sheet_by_index(0) | |
for row in range(s.nrows): | |
data = [] | |
for col in range(s.ncols): | |
value = s.cell(row, col).value | |
data.append(value) | |
datum.append(data) | |
return datum | |
def write_excel(excel): | |
"""Write some 中文 into excel, which is supposed to be an excel.""" | |
from xlwt import Workbook | |
wb = Workbook() | |
s = wb.add_sheet('sheet1') | |
s.write(0, 0, u'我是一串中文,来自(0, 0)') | |
s.write(1, 0, u'我是另一串中文,来自(1, 0)') | |
s.write(0, 1, u'我是一串中文,来自(0, 1)') | |
s.write(1, 1, u'我是另一串中文,来自(1, 1)') | |
wb.save(excel) | |
def test(): | |
excel = 'test.xls' | |
write_excel(excel) | |
datum = read_excel(excel); | |
for rows in datum: | |
for value in rows: | |
print value, '\t', | |
if __name__ == '__main__': | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment