Python读excel生成数据存入txt文件
2016-09-21 21:43
716 查看
我的excel文件结构:
学习了xlrd如何操作excel文件、python读写txt文件、jason.dumps()转换dict为string类型之后,进行了第一次尝试。
第一次尝试:
通过第一次尝试的代码得到了上面的内容(内容其实已经保存在txt里面,打印在屏幕上是为了方便查看结果),得出如下结论:
数字的字面类型与excel里面的类型不一致;
没有按照顺序存储数据,这主要是dict类型不分先后的原因,对于数据影响不大,但可读性较差。
接下来主要解决问题1:
这次得到了相对正确的数字,当我修改了我的excel文件内容时:
根据测试,判断我对excel中的获得的浮点数的转换基本是对的,但也不能保证完全正确。
上面的代码每次遇到这种数字都需要做判断,我希望能够定义一个函数,可以重复用于转换数据
显然是定义函数的代码写在了调用函数部分的内容下面造成的,然而人家JavaScript是可以这么干的,不爽!
成功!
最后的疑问:
关于如何判断字符串"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之间转换】
学习了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之间转换】
相关文章推荐
- python读取excel中表结构生成sql语句,存入txt文件
- Python中生成(写入数据到)Excel文件
- 将excel表中的数据写到txt文本文件的Python脚本
- Python--将 txt文件里的-字典类型-的数据写到Excel中--xlwt--TxtToExcel
- Python--使用xlwt将列表类型的数据写到Excel xls文件中--TxtToExcel
- ArcGIS学习记录-Excel和Txt中XY点数据生成点Shape文件方法
- 将大量有规律txt文本数据转换成xml格式,在导入excel,生成excel文件,在导入spass中,对数据进行分析
- [VB.NET]如何将DataGridView中的数据存入Excel文件中?
- ASP.NET中上传并读取Excel文件数据,附后生成EXCEL及杀掉EXCEL进程。
- 生成了一个txt文件,如何用记事本和EXCEL 打开它给用户看?
- 将上传文件以二进制形式存入数据库中,并下载数据库中的二进制数据生成对应的文件
- txt数据格式化导入excel文件中
- asp.net从数据库导出数据到word、excel、txt文本文件
- ASP.NET中上传并读取Excel文件数据,附后生成EXCEL及杀掉EXCEL进程。
- 使用JCom动态生成Excel文件,并向其中填入数据
- Python:生成特定内容的数据文件
- ASP.NET 将 DataView 数据导出到服务器上生成Excel文件
- 将Excel或者Txt文件中的数据导入Oracle数据库
- ASP.NET中上传并读取Excel文件数据,附后生成EXCEL及杀掉EXCEL进程
- SQL 数据的导入导出,对远程(MSsql,OracleAccess,)数据库的操作以及读取Excel,txt文件中的数据