我的excel文件结构:
学习了xlrd如何操作excel文件、python读写txt文件、jason.dumps()转换dict为string类型之后,进行了第一次尝试。
第一次尝试:
import xlrd
import json
data = xlrd.open_workbook('test.xlsx')#打开excel文件
table = data.sheet_by_name(u'Sheet1')#通过名称获取excel表
nrows = table.nrows
final_obj={}
for i in range(nrows):
cur_row = table.row_values(i)
first = str(cur_row[0])
if first:
final_obj[first] = []
for j in range(1,len(cur_row)):
if cur_row[j]:
final_obj[first].append(cur_row[j])
final_str = json.dumps(final_obj)
print(final_str)
file = open('test.txt', 'w')
file.write(final_str)
file.close()
通过第一次尝试的代码得到了上面的内容(内容其实已经保存在txt里面,打印在屏幕上是为了方便查看结果),得出如下结论:
- 数字的字面类型与excel里面的类型不一致;
- 没有按照顺序存储数据,这主要是dict类型不分先后的原因,对于数据影响不大,但可读性较差。
接下来主要解决问题1:
import xlrd
import json
data = xlrd.open_workbook('test.xlsx')#打开excel文件
table = data.sheet_by_name(u'Sheet1')#通过名称获取excel表
nrows = table.nrows
final_obj={}
for i in range(nrows):
cur_row = table.row_values(i)
#我已经通过pirnt(cur_row[0])得知excel中的数字获得后为浮点型,判断它是否为浮点型且能够整除
if type(cur_row[0]) == type(1.1) and cur_row[0]%1 == 0:
first = str(int(cur_row[0]))
else:
first = str(cur_row[0])
if first:
final_obj[first] = []
for j in range(1,len(cur_row)):
if cur_row[j]:
if type(cur_row[j]) == type(1.1) and cur_row[j]%1 == 0:
cur_value = str(int(cur_row[j]))
else:
cur_value = str(cur_row[j])
final_obj[first].append(cur_value)
final_str = json.dumps(final_obj)
print(final_str)
file = open('test.txt', 'w')
file.write(final_str)
file.close()
这次得到了相对正确的数字,当我修改了我的excel文件内容时:
根据测试,判断我对excel中的获得的浮点数的转换基本是对的,但也不能保证完全正确。
上面的代码每次遇到这种数字都需要做判断,我希望能够定义一个函数,可以重复用于转换数据
import xlrd
import json
data = xlrd.open_workbook('test.xlsx')#打开excel文件
table = data.sheet_by_name(u'Sheet1')#通过名称获取excel表
nrows = table.nrows
final_obj={}
for i in range(nrows):
cur_row = table.row_values(i)
first = toIntString(cur_row[0])
if first:
final_obj[first] = []
for j in range(1,len(cur_row)):
if cur_row[j]:
final_obj[first].append(toIntString(cur_row[j]))
final_str = json.dumps(final_obj)
print(final_str)
file = open('test.txt', 'w')
file.write(final_str)
file.close()
def toIntString(value):
result = ""
if type(value) == type(1.1) and value%1 == 0:
result = str(int(value))
else:
result = str(value)
return result
显然是定义函数的代码写在了调用函数部分的内容下面造成的,然而人家JavaScript是可以这么干的,不爽!
import xlrd
import json
def toIntString(value):
result = ""
if type(value) == type(1.1) and value%1 == 0:
result = str(int(value))
else:
result = str(value)
return result
data = xlrd.open_workbook('test.xlsx')#打开excel文件
table = data.sheet_by_name(u'Sheet1')#通过名称获取excel表
nrows = table.nrows
final_obj={}
for i in range(nrows):
cur_row = table.row_values(i)
first = toIntString(cur_row[0])
if first:
final_obj[first] = []
for j in range(1,len(cur_row)):
if cur_row[j]:
final_obj[first].append(toIntString(cur_row[j]))
final_str = json.dumps(final_obj)
print(final_str)
file = open('test.txt', 'w')
file.write(final_str)
file.close()
成功!
最后的疑问:
- 关于如何判断字符串"1.0"或"abc"(二者均有可能出现)为整数我没有找到合适的解决方法,难道只能用try…except…来解决int(str)造成的异常么?
- 结果应该为0.2:
- 一个不和谐的方法eval(),本来打算用来转换数字的:
- python里面的除法总是保留1位小数:
参考:
- https://my.oschina.net/u/1165991/blog/742587
- http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html【python操作Excel读写--使用xlrd】
- http://blog.sina.com.cn/s/blog_4ddef8f80102v8af.html【JSON: Python Objects与String之间转换】