Python新手数据嵌套问题,python新手嵌套,大家好,我是一个新手py
Python新手数据嵌套问题,python新手嵌套,大家好,我是一个新手py
大家好,我是一个新手python初学者,现在我正在做一个小程序读写xlsx文件。我有提到一些代码,似乎我有一些问题要处理这个程序中的数据。
我想求教一下,关于数据嵌套的问题,我是新手
# -*- coding: utf-8 -*- import xdrlib,sysimport xlrdimport xlwtdef open_excel(file= 'incomingtry.xlsx'): try: data = xlrd.open_workbook(file) return data except Exception,e: print str(e)def excel_table_byindex(file= 'incomingtry.xlsx',colnameindex=0,by_index=0): data = open_excel(file) table = data.sheets()[by_index] nrows = table.nrows ncols = table.ncols colnames = table.row_values(colnameindex) list =[] for rownum in range(1,nrows):row = table.row_values(rownum) if row: app = {} for i in range(len(colnames)): app[colnames[i]] = row[i] list.append(app) return listdef excel_table_byname(file='incomingtry.xlsx',colnameindex=0,by_name=u'Sheet1'): data = open_excel(file) table = data.sheet_by_name(by_name) nrows = table.nrows colnames = table.row_values(colnameindex) list =[] for rownum in range(1,nrows): row = table.row_values(rownum) if row: app = {} for i in range(len(colnames)): app[colnames[i]] = row[i] list.append(app) return listdef creat_the_newfile(): wbk = xlwt.Workbook() sheet = wbk.add_sheet('sheet 1')def main(): tables = excel_table_byindex() #print tables for row in tables: print row #print "%d",row['order']; #for i in range(len(tables)): # print row{i} print 'I have',len(tables) print 'The 3rd row ist',tables[2] Total_rownum=len(row) print 'The lenght of row ist',Total_rownumif __name__=="__main__": main()
我看到了,这个tables是个list,而row则是dict,正确么?现在我如何访问或者操作list tables中row的每一个元素呢?
谢谢帮助
for this code, in the final step, I noticed that the tables is a list, but the row is dict embeded in the tables, so my question is, how can I visit the elements in all row and operate them?
thanks for your helping!
搞不清楚你要问什么?dict就用dict的函数
for i in dict: print(dict[i])
想针对整个list操作可以用numpy/pandas,一维是列表,二维是字典,用pandas更方便,但估计你暂时还没能力学这个,先搞清dict吧
编橙之家文章,
相关内容
- pypi中没有tkinter吗?难道需要手动安装,pypitkinter安装,
- Python递归函数时间复杂度如何判断,python递归函数判断
- python开发的微信企业付款出现CA证书error,pythonca,我在做
- Python sqlalchemy批量数据插入优化注意事项有哪些?,pyt
- Python程序里用pdb方法进行调试时出现错误,pythonpdb,调试
- Python序列sort与bisect.insort及heapq.heappush哪种效率最高效,
- Python有大数据处理性能优化好方法吗,python性能优化
- python requests发送post Page load erro问题,requestserro,post过去
- 想用powershell写命令完成启动软件和调用python脚本怎么做
- mac电脑不支持gbk,python post数据出现乱码怎么破?,gbkpy
评论关闭