python 3.6 MJ小工具

2017.07.14 update

做了个界面,不需要使用cmd命令行+文件路径的方式来使用了;

链接如下:

http://www.cnblogs.com/chenyuebai/p/7150382.html

 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - -原博文- - - - - - - - - - - - - - - - - - - - - - - - - - - - -

周末给妹子做的小工具集;大体框架差不多了,支持执行传入参数,从而指定功能;

再写个readme放桌面上,里面描述命令对应功能,把命令行粘贴到cmd命令台,回车执行即可;     #原始文件路径写死了(放她PC桌面上)

                                               #common_tools.py路径配到环境变量里

如:1.格式化手机号: python common_tools.py 1

  2.XXXXXX功能: python common_tools.py 2

  ...

目前就一个功能:批量格式化手机号,适配她公司短信群发平台

详细涉及:

(1)文件读取  (2)数据格式化  (3)短信群发平台每次仅支持50人,需要适配  (4)支持默认收件人  (5)异常手机号提示  (6)自动将前50拷贝到剪切板【未实现】

另外后续可能加其他功能,需要支持外部参数传入,指定某功能

 

#20170613更新:自动将执行结果拷贝至剪切板

代码:

 
  
################################################################# 
#author: 陈月白
#_blogs: http://www.cnblogs.com/chenyuebai/
#################################################################

#
coding: utf -8 import sys import traceback import pyperclip class COMMON_TOOLS(): def __init__(self): #原始数据文件 self.data_file = r"C:\Apps\install_zmj\workspace_ch\cfg\data.txt" #函数表 self.function_map = { "1":"format_msg_receiver" } #参数转function def args_trans2function(self,argCode): if argCode: try: function_name = self.function_map[argCode] #print("INFO:function_name =",function_name) return function_name except: print("ERROR:code:%s no this function,plz connect CHEN...输入函数代码不在此工具的函数列表中,请联系陈"%argCode) #拉起功能函数 def start_execute_function(self,function_name): if function_2_exe == "format_msg_receiver": try: print("now begin to execute:",function_2_exe) data = open(self.data_file, "r", encoding="UTF-8") self.format_msg_receiver(data) except: traceback.print_exc() print("ERROR:begin exe format_msg_receiver failed") #格式化短信收件人 def format_msg_receiver(self,receiver_info): #配置默认收件人 result = "132******96,135******23" flag = 0 if receiver_info: try: for receiver_num in receiver_info.readlines(): #print(receiver_num) #原始数据格式检查是否为手机号;如果不是11位,且不是空,打出错误信息;空值过滤 if not len(receiver_num.replace("\n","")) == 11: if not receiver_num.replace("\n","") == "": print("WARNING:手机号异常--> %s 建议人工修改最后输出结果 或原始文件:data.txt"%(receiver_num.replace("\n",""))) #格式化,空值跳过 if not receiver_num.replace("\n","") == "": #print("receiver_num =",receiver_num) result = result + "," + receiver_num.replace("\n","") flag = flag + 1 print("\nINFO:格式化联系人完成,共格式化联系人 %s 个 提示:最大支持100个"%flag) if result: result_list = result.split(",") #print("result_list =",result_list) if len(result_list) <= 50: print("INFO:最终结果如下:\n") print(result) #拷贝内容至剪切板 try: self.copy_2_clipboard(result) print("\n【结果已自动拷贝至剪切板,直接粘贴即可】\n") except: pass #适配群发短信工具,大于50人处理,支持最大100个联系人 else: #处理前50个 result_list_50 = result_list[0:50] result_50_out = "" flag_50 = 0 #print("result_list_50 =",result_list_50) for i in result_list_50: if flag_50 == 0: result_50_out = result_50_out + i flag_50 = flag_50 + 1 else: result_50_out = result_50_out + "," + i flag_50 = flag_50 + 1 #处理剩余部分 result_list_other = result_list[50:] flag_other = 0 result_other_out = "" for i in result_list_other: if flag_other == 0: result_other_out = result_other_out + i flag_other = flag_other + 1 else: result_other_out = result_other_out + "," + i flag_other = flag_other + 1 print("INFO:适配群发短信工具,前50个为:\n%s\n"%result_50_out) #拷贝内容至剪切板 try: self.copy_2_clipboard(result_50_out) print("【上述前50个已自动拷贝至剪切板,直接粘贴即可】\n") except: pass print("INFO:其他部分为:\n%s\n"%result_other_out) return 0 except: traceback.print_exc() print("ERROR:function format_msg_receiver execute failed!\n 请联系陈") #拷贝str至剪切板 def copy_2_clipboard(self,str_data): try: if str_data: pyperclip.copy(str_data) #print("INFO:拷贝至剪切板成功") except: traceback.print_exc() #print("ERROR:拷贝至剪切板失败") def main(): global function_2_exe print("\n--------------------------开始执行--------------------------") ZMJ = COMMON_TOOLS() #获取函数 try: code_input = sys.argv[1] print("INFO:code_input =",code_input) function_2_exe = ZMJ.args_trans2function(code_input) print("INFO:function_2_exe =",function_2_exe) except: traceback.print_exc() print("ERROR:请输出功能参数") print(r"help info in C:\Apps\install_zmj\workspace_ch\cfg\readme.txt") #执行 if function_2_exe: ZMJ.start_execute_function(function_2_exe) else: print("ERROR:function_2_exe为空,code获取函数名失败") print("--------------------------执行完成--------------------------\n") #入口 main()

 

 

 

执行:

1.原始联系人数据拷贝到指定文件下

2.从readme中拷贝"python common_tools.py 1"至cmd,执行

 

 

 

未完待续。。。

 

转载于:https://www.cnblogs.com/chenyuebai/p/6999561.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值