楼主: 1244690926
6078 14

python def 函数传送动态变量 [推广有奖]

  • 0关注
  • 0粉丝

博士生

22%

还不是VIP/贵宾

-

威望
0
论坛币
231 个
通用积分
80.7137
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
27845 点
帖子
57
精华
0
在线时间
404 小时
注册时间
2012-11-24
最后登录
2024-11-15

楼主
1244690926 发表于 2022-4-8 17:04:04 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
例如:有1个字符串A='RES1,RES2,RES3,RES4',其中字符串里面RES1=1,RES2=2,RES3=8,RES4=9,目前需要
把字符传参数传入函数

例子:
def get_sel_excel(file_excel):
    w_excel(A)


其中A入参该怎么传?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:python def EXCEL xcel exce

沙发
1244690926 发表于 2022-4-8 17:05:20
def get_sel_excel(file_excel):
    w_excel(RES1,RES2,RES3,RES4)

目前要表示如上所示

藤椅
HappyAndy_Lo 发表于 2022-4-9 09:04:13
def get_sel_excel(file_excel, res1, res2, res3, res4):
     w_excel(res1, res2, res3, res4)

如果嫌参数长,可以放进列表listRes =[1,2,8, 9]
def get_sel_excel(file_excel, listRes):
       w_excel(listRes[0], listRes[1], listRes[2], listRes[3])

板凳
1244690926 发表于 2022-4-9 10:39:54
HappyAndy_Lo 发表于 2022-4-9 09:04
def get_sel_excel(file_excel, res1, res2, res3, res4):
     w_excel(res1, res2, res3, res4)
可能我描述得不够准确,现成是得出变量比如remind_s1,remind_res1,remind_s2,....等一串变量,这串变量都是有值的,而且现在是不确定变量的个数,这个是动态的变量个数,搞成字符串AD=remind_s1+remind_res1+remind_s2+...传送到def get_sel_excel(AD),发现这样子会报错,行不通,有什么其他方法可以传送这个动态个数的的变量

报纸
HappyAndy_Lo 发表于 2022-4-9 12:48:42
AD = [remind_s1, remind_res1, ....]
def get_sel_excel(AD):
那应该没问题。。。

地板
1244690926 发表于 2022-4-9 15:07:32
HappyAndy_Lo 发表于 2022-4-9 12:48
AD = [remind_s1, remind_res1, ....]
def get_sel_excel(AD):
那应该没问题。。。
那这个就是列表了,列表试了也不行

7
HappyAndy_Lo 发表于 2022-4-10 00:17:16
贴完整代码看看?

8
xjg 发表于 2022-4-10 17:23:01
函数中参数的传递,有两种方式: 值传递和引用传递。你要传递一个可变对象(即你说的动态变量), 必须用“引用传递” 的方法。 在其他语言中(比如 C++), 你可以显式地指定是 “值传递”还是“引用传递”。 在python中, 不能显式地指定。 但规定了对“可变对象”(mutable object)函数中参数的传递方式是:引用传递。 而对“不可变对象” (immutable object), 传递方式是: 值传递。

楼上有人建议你用 list,  这是对的。 list 是一个可变对象。 能解决你的问题。

9
xjg 发表于 2022-4-10 17:36:39
要是你不想改变原来的对象, 用“值传递” 比如用 immutable object: tuple 也能解决你的问题。

10
1244690926 发表于 2022-4-11 17:54:12
HappyAndy_Lo 发表于 2022-4-10 00:17
贴完整代码看看?
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 28 17:05:41 2021

@author: win10
"""



import pymysql
import datetime
from datetime import date
# from openpyxl import load_workbook
# from openpyxl.utils import get_column_letter
import copy
import pandas as pd
# import xlrd
# import copy
import psutil
import os
import xlrd, xlwt
from xlutils.copy import copy as xl_copy
from win32com.client import Dispatch
from win32com.client import DispatchEx
import win32com
import win32com.client as win32

road = 'C:\\Users\\win10\\Desktop\\报表\\'
time =(datetime.datetime.now()+ datetime.timedelta(days=0)).strftime('%Y-%m-%d')
date =(datetime.datetime.now()+ datetime.timedelta(days=0)).strftime('%m%d')


# 建立连接
conn = pymysql.connect(
host = "***",
port = 3306,
user = "***",
passwd = "***",
db = "***",
charset = "utf8"
)



# 建立游标
cursor = conn.cursor()
cursor.execute("CALL NGN_CASE_SUM();") #执行查询语句
res1 = cursor.fetchall()


remind_s=pd.read_sql("SELECT COUNT(*) RD_GP_S FROM NGN_CASE_SUM WHERE ORG='4000000003' AND TASK_TYPE_ID LIKE '%RD_GP_S%' ",conn)
remind_s=remind_s['RD_GP_S'].tolist()
remind_s_zb=pd.read_sql("SELECT COUNT(DISTINCT POSITION_ID) RD_GP_S_ZB FROM NGN_CASE_SUM WHERE ORG='4000000003' AND TASK_TYPE_ID LIKE '%RD_GP_S%' ",conn)
remind_s_zz=pd.read_sql("SELECT DISTINCT(SUBSTRING_INDEX(SUBSTRING_INDEX(POSITION_NAME,'_',-1),'(',1)) RD_GP_S_ZZ FROM NGN_CASE_SUM WHERE ORG='4000000003' AND TASK_TYPE_ID LIKE '%RD_GP_S%' ",conn)
remind_s_jd=pd.read_sql("SELECT COUNT(DISTINCT TASK_TYPE_ID) RD_GP_S_JD FROM NGN_CASE_SUM WHERE ORG='4000000003' AND TASK_TYPE_ID LIKE '%RD_GP_S%' ",conn)



# 转换为数字
remind_s_zb=remind_s_zb['RD_GP_S_ZB'].tolist()
remind_s_zz=remind_s_zz['RD_GP_S_ZZ'].tolist()
remind_s_jd=remind_s_jd['RD_GP_S_JD'].tolist()

i=0
sd1=''
sd2=''
for i in range(0,remind_s_zb[0]):
    locals()["remind_s"+str(i+1)] = remind_s_zz  #生成动态变量 "qq"+str(a)
    locals()["sql_s"+str(i+1)]="SELECT * FROM NGN_CASE_SUM WHERE ORG='4000000003' AND TASK_TYPE_ID LIKE '%RD_GP_S%' AND SUBSTRING_INDEX(SUBSTRING_INDEX(POSITION_NAME,'_',-1),'(',1)='"+remind_s_zz+"' ORDER BY CURR_TASK_COUNT DESC"
    cursor = conn.cursor()
    cursor.execute(locals()["sql_s"+str(i+1)]) #执行查询语句
    locals()["remind_s_res"+str(i+1)] = cursor.fetchall()
    if i==remind_s_zb[0]-1:
        sd1+="remind_s"+str(i+1)+','
        sd2+="remind_s_res"+str(i+1)+','
    else:
        sd1+="remind_s"+str(i+1)+','
        sd2+="remind_s_res"+str(i+1)+','
i+=1
conn.commit()
conn.close()



def get_sel_excel(file_excel):
    w_excel()
   
#操作excel
def w_excel():

   
   
    # open existing workbook
    rb = xlrd.open_workbook(road+'数据.xls', formatting_info=True)
    # make a copy of it
    wb = xl_copy(rb)

    title1 = ['当日数据表']
   


    sheet.write(0,0,xlwt.Formula('NOW()'),style1)
    sheet.write(1,0,title2,style0)
    sheet.write_merge(0,1,1,7,title1,style0)
   
    # 表头
    i=1
    for header in title3:
        if i<=8:
           sheet.write(2,i-1,header,style0)
        else:
           sheet.write(2,i-1,header,style2)
        i+=1
        
    if remind_s_jd[0]:  
        # 第一列
        sheet.write_merge(3,remind_s[0]+remind_s_zb[0]+3,0,0,title4[0],style0)
        # 最后一行总计
        sheet.write_merge(remind_s[0]+remind_s_zb[0]+3,remind_s[0]+remind_s_zb[0]+3,1,3,'总计',style0)
        
        
        # 组长
        i=0
        t=0
        for i in range(0,remind_s_zb[0]):
            if  i==0:
                sheet.write_merge(3,len(locals()["remind_s_res"+str(i+1)])+3,1,1,locals()["remind_s"+str(i+1)],style0)        
            else:
                sheet.write_merge(t+3,t+len(locals()["remind_s_res"+str(i+1)])+3,1,1,locals()["remind_s"+str(i+1)],style0)
            t+=len(locals()["remind_s_res"+str(i+1)])+1
            i+=1
        
            
        # 各组数据
        i=0
        ct=0
        t4=''
        t5=''
        t6=''
        # 写入各组数据
        for i in range(0,remind_s_zb[0]):
            for row in range(0,len(locals()["remind_s_res"+str(i+1)])):
                for col in range(0,len(locals()["remind_s_res"+str(i+1)][row])):
                    if col==3 or col==4:
                        if i==0:
                           sheet.write(row+ct+3,col+2,locals()["remind_s_res"+str(i+1)][row][col],style7)
                        else:
                           sheet.write(row+ct+3,col+2,locals()["remind_s_res"+str(i+1)][row][col],style7)   
                    else:
                        if i==0:
                           sheet.write(row+ct+3,col+2,locals()["remind_s_res"+str(i+1)][row][col],style6)
                        else:
                           sheet.write(row+ct+3,col+2,locals()["remind_s_res"+str(i+1)][row][col],style6)   
                row+=1
            col+=1
        # 各组总计
            if i==0:
                sheet.write_merge(len(locals()["remind_s_res"+str(i+1)])+3,len(locals()["remind_s_res"+str(i+1)])+3,2,3,'总计',style0)
                sheet.write(len(locals()["remind_s_res"+str(i+1)])+3, 4,xlwt.Formula('SUM(E4:E'+str(len(locals()["remind_s_res"+str(i+1)])+3)+')'),style0)
                sheet.write(len(locals()["remind_s_res"+str(i+1)])+3, 5,xlwt.Formula('SUM(F4:F'+str(len(locals()["remind_s_res"+str(i+1)])+3)+')'),style0)
                sheet.write(len(locals()["remind_s_res"+str(i+1)])+3, 6,xlwt.Formula('SUM(G4:G'+str(len(locals()["remind_s_res"+str(i+1)])+3)+')'),style0)
   
            else:
                sheet.write_merge(ct+len(locals()["remind_s_res"+str(i+1)])+3,ct+len(locals()["remind_s_res"+str(i+1)])+3,2,3,'总计',style0)
                sheet.write(ct+len(locals()["remind_s_res"+str(i+1)])+3,4,xlwt.Formula('SUM(E'+str(ct+3+1)+':E'+str(ct+len(locals()["remind_s_res"+str(i+1)])+3)+')'),style0)
                sheet.write(ct+len(locals()["remind_s_res"+str(i+1)])+3,5,xlwt.Formula('SUM(F'+str(ct+3+1)+':F'+str(ct+len(locals()["remind_s_res"+str(i+1)])+3)+')'),style0)
                sheet.write(ct+len(locals()["remind_s_res"+str(i+1)])+3,6,xlwt.Formula('SUM(G'+str(ct+3+1)+':G'+str(ct+len(locals()["remind_s_res"+str(i+1)])+3)+')'),style0)
               
            ct+=len(locals()["remind_s_res"+str(i+1)])+1
            if i==0:
               t4+='E'+str(len(locals()["remind_s_res"+str(i+1)])+remind_s_jd[0]+3)
               t5+='F'+str(len(locals()["remind_s_res"+str(i+1)])+remind_s_jd[0]+3)
               t6+='G'+str(len(locals()["remind_s_res"+str(i+1)])+remind_s_jd[0]+3)
            else:
               t4+=',E'+str(ct+3)
               t5+=',F'+str(ct+3)
               t6+=',G'+str(ct+3)
            if i==remind_s_zb[0]-1:
               sheet.write(ct+3,4,xlwt.Formula('SUM('+t4+')'),style0)
               sheet.write(ct+3,5,xlwt.Formula('SUM('+t5+')'),style0)
               sheet.write(ct+3,6,xlwt.Formula('SUM('+t6+')'),style0)
            i+=1  

        
    wb.save(road+'数据'+'.xls')
    print("导出成功!")
   
if __name__ == "__main__":
    file_excel = road+'数据'+'.xls'
    get_sel_excel(file_excel)







您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-28 15:06