python获取excel中的值

该代码读取Excel文件中的多个sheet,提取表名、字段名和属性,然后使用pandas创建DataFrame。它还生成建表SQL,可能涉及创建视图和存储过程。此外,有一个函数用于创建文件夹路径,并将数据写入TXT文件。
摘要由CSDN通过智能技术生成
import pandas as pd
import createPath  as cp
import createTable as ct
# pip --default-timeout=1688 install openpyxl -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
excelPath=r'C:\Users\admin\Desktop\mapping.xlsx'
#加载excel所有sheet
excelData=pd.read_excel(excelPath,sheet_name=None)

# df = pd.DataFrame(pd.read_excel(r'C:\Users\admin\Desktop\mapping.xlsx'))
# print(excelData.keys())
#创建文件夹path
cp.createPath()
for sheet in excelData.keys():
    # print(sheet)
    print(excelData[sheet])
    sheetDF = pd.DataFrame(excelData[sheet])
    height = sheetDF.shape[0]
    # print(height)
    if height-2>1:
        # print(sheetDF.iloc[height-2,1])
        # print(sheetDF[height-4])
        #dwi表名
        dwi=sheetDF.iloc[2,1]
        #dwi表名中文
        dwiName=sheetDF.iloc[2,2]
        #sdi表名
        sdiName = sheetDF.iloc[2,15]
        # print(sdiName.split('\n')[0])
        #dwi字段名称
        dwiField=sheetDF.iloc[8:height-1, 1].values.tolist()
        print(dwiField)
        #dwi字段属性
        dwiProperty=sheetDF.iloc[8:height-1, 2].values.tolist()
        print(dwiProperty)
        #sdi字段处理逻辑
        sdiLogic=sheetDF.iloc[8:height-1, 16].values.tolist()

        #生成建表sql
        ct.createTable(dwi, '', '', dwiField, dwiProperty)
        #生成创建视图sql
        #生成存储过程sql
import os
#这个函数的作用是对传入的值进行加2操作
def createPath():
    print('ok!!!!!!!!!!')
    if not os.path.exists(r'C:\Users\admin\Desktop\testMapping\001'):
        os.mkdir(r'C:\Users\admin\Desktop\testMapping\001')

def createTable(dwi,dwiName,sdiName,dwiField,dwiProperty):
    # 定义要写入txt的内容
    content = "Hello world! This is a %s \n"% (dwi)
    for i in range(len(dwiField)):
        if i<len(dwiField)-1:
            content += "%s %s ,\n"% (str(dwiField[i]),str(dwiProperty[i]))
        else:
            content += "%s %s " % (str(dwiField[i]), str(dwiProperty[i]))
    # 打开txt文件,文件路径和文件名需要根据实际情况修改
    with open(r"C:\Users\admin\Desktop\testMapping\001\%s.txt"% (dwi), "w") as f:
        # 将内容写入文件中
        f.write(content)

    # 提示写入成功
    print("Content has been written to %s.txt"% (dwi))
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值