《xls json csv 文件读取》

#coding=utf-8
import xlrd
import json
import csv

#地址前用'\'转译符要加
workbook=xlrd.open_workbook('D:/untitled/1022/date.xls')
#提取表格名称
sheets=workbook.sheet_names()
#提取第一个表格内容
worksheet=workbook.sheet_by_name((sheets[0]))

#统计有多少行
worksheet.nrows
#统计有多少列
worksheet.ncols
#遍历所以单元格
for i in range(worksheet.nrows):
    for j in range(worksheet.ncols):
        print(worksheet.cell_value(i,j),'\t',end='')
    print('')

#json load(filed)出来直接是对象
with open('D:/untitled/1022/date.json','r',encoding='utf-8') as filed:
    filedd = json.load(filed)
    print(filedd)

#csv.DictReader(fils)出来是首行对应值字典
with open('D:/untitled/1022/date.csv','r',encoding='utf-8') as fils:
    global jsonlist
    jsonlist = []
    reader=csv.DictReader(fils)
    for i in reader:
        print(i)
        #转换成字典
        print(dict(i))
        jsonlist.append(dict(i))

with open('D:/untitled/1022/date.csv','r',encoding='utf-8') as fils:
    #不能提取第一行
    reader=csv.reader(fils)
    for i in reader:
        print(i)

猜你喜欢

转载自www.cnblogs.com/huazhou695/p/9885810.html