用Python遍历Excel的行和列,再写入一个文件

import pandas as pd

import numpy as np

filename ='demo_text.sql'

# 读取excel

def readExcel(excelPath, sheet_name):

df = pd.read_excel(excelPath, sheet_name=sheet_name, header=None)

# 最大行

    nrows = df.shape[0]

print("行数:\n" +str(nrows))

# 最大列

    ncols = df.columns.size

print("列数:\n" +str(ncols))

for rowIndexin range(nrows):

source = df.iloc[rowIndex, 1]

npp = df.iloc[rowIndex, 2]

fuseCode = df.iloc[rowIndex, 3]

newFuseCode = df.iloc[rowIndex, 5]

sql =""

        print("source:" +str(source) +",npp:" +str(npp) +",fuseCode:" +str(fuseCode) +",newFuseCode:" +str(newFuseCode) +"\n")

if str(fuseCode) =="-" and str(npp) !="-":

if str(source) =="Others":

if str(newFuseCode) =="-":

sql ="update demo_table1 set reason = 'CancelOtherReInputReason' where fuse_policy_code = '" +str(

fuseCode) +"' ;"

                else:

sql ="update demo_table1 set new_fuse_code ='" +str(

newFuseCode).strip() +"',reason = 'CancelOtherReInputReason' where fuse_policy_code = '" +str(

fuseCode) +"' ;"

            if str(source) =="BACKDOOR" or str(source) =="Backdoor":

if str(newFuseCode) =="-":

sql ="update demo_table2 set reason = 'CancelOtherReInputReason' where npp = '" +str(npp) +"' ;"

              else:

sql ="update demo_table2 set new_fuse_code ='" +str(newFuseCode).strip() +"',reason = 'CancelOtherReInputReason' where npp = '" +str(npp) +"' ;"

            if str(source) =="Offline" or str(source) =="OFFLINE":

if str(newFuseCode) =="-":

sql ="update demo_table3 set reason = 'CancelOtherReInputReason' where associated_policy_code = '" +str(

npp) +"' ;"

                    sql = sql +"\n" +"update demo_table4 set reason = 'CancelOtherReInputReason' where associated_policy_code = '" +str(

npp) +"' ;"

                else:

sql ="update demo_table3 set new_fuse_code ='" +str(

newFuseCode).strip() +"',reason = 'CancelOtherReInputReason' where associated_policy_code = '" +str(

npp) +"' ;"

                    sql = sql +"\n" +"update demo_table4 set new_fuse_code ='" +str(newFuseCode).strip() +"',reason = 'CancelOtherReInputReason' where associated_policy_code = '" +str(

npp) +"' ;"

            print("==================================\n")

print(sql)

txt_file =open(filename, "a", encoding="utf-8")# 以写的格式打开先打开文件

            txt_file.write(sql)

txt_file.write("\n")

txt_file.close()

# Main 入口

if __name__ =="__main__":

print("this is a excel \n")

readExcel("/Users/xiaobao/Desktop/rev_ops-manual.xlsx", "Sheet2")

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值