码迷,mamicode.com
首页 > Web开发 > 详细

pyhton读取xsls文件与json文件练习

时间:2020-01-03 00:13:17      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:import   info   文件   use   excel   self   编写   练习   学习   

今天python编写了python读取excel文件与json文件的写法,其中细节还需研究。

阅读曾国藩自传30+,预计这周周末读完。

每天都在努力

不足:python学习不够深入,仍需努力。

 

读取excel

#!/usr/bin/python
#-*- coding:UTF-8 -*-
import xlrd
class OperationExcel:
def __init__(self,file_name=None,sheet_id=None):
if file_name:
self.file_name = file_name
self.sheet_id = sheet_id
else:
self.file_name = ‘../dataconfig/interface.xlsx‘
self.sheet_id = 0
self.data = self.get_data()
def get_data(self):
data = xlrd.open_workbook(self.file_name)
tables = data.sheets()[self.sheet_id]
return tables
#获取单元格的行数
def get_lines(self):
tables = self.data
return tables.nrows
#获取某个单元格的内容
def get_cell_value(self,row,col):
return self.data.cell_value(row,col)

if __name__ == "__main__":
opers = OperationExcel()
print (opers.get_data().nrows)
print(opers.get_lines())
print(opers.get_cell_value(1,1))

excel文件

技术图片

 

运行结果:

4
4
http://10.10.19.90

 

读取json

import json
fp = open("../dataconfig/login.json")
data = json.load(fp)
print(data[‘login‘])

class OperationJson:
def __init__(self):
self.data = self.read_data()
#读取json文件
def read_data(self):
with open("../dataconfig/login.json") as fp:
data = json.load(fp)
return data
#读取字段
def get_data(self,id):
return self.data[id]
if __name__=="__main__":
opjson = OperationJson()
print(opjson.get_data("loginout"))

json文件如下:

{"login":{"username":"hwj","password":"1111111"},
"loginout":{"username":"hwj","password":"1111111"},
"addcart":{"username":"hwj","password":"1111111"}
}


{‘username‘: ‘hwj‘, ‘password‘: ‘1111111‘}
{‘username‘: ‘hwj‘, ‘password‘: ‘1111111‘}

pyhton读取xsls文件与json文件练习

标签:import   info   文件   use   excel   self   编写   练习   学习   

原文地址:https://www.cnblogs.com/wenwen9090/p/12142686.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!