吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4498|回复: 29
收起左侧

[Python 原创] 【源码】【写给女友的软件①】批量匹配软件(类似Vlookup函数)

  [复制链接]
wkdxz 发表于 2022-10-11 16:18
本帖最后由 wkdxz 于 2022-10-14 14:37 编辑

软件介绍:成品下载页


成品下载: https://www.52pojie.cn/thread-1697081-1-1.html

软件界面:





操作演示:(差不多,也就不更新了)


使用Python字典匹配,按制表符tab分割源数据,如果分隔符不是tab,则默认按空格分割
使用 direction 切换匹配的方向是从左到右,还是右到左
[Python] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
def data_source_dic(shujuyuan, direction=True, fenge='\t'):
    if not (shujuyuan := shujuyuan.strip()):
        return
    data_source = shujuyuan.split('\n')
    if fenge not in data_source[0]:
        fenge = ' '
    data_dic = {}
    with contextlib.suppress():
        for i in data_source:
            if i := i.strip():
                fen_list = i.split(fenge)
                if len(fen_list) > 1#处理单列的情况
                    if direction:
                        k, *_, v = fen_list  #左→右
                    else:
                        v, *_, k = fen_list  #右←左
                else:
                    k = fen_list[0]
                    v = ''
                if k not in data_dic:
                    data_dic[k.strip()] = v.strip()
        return data_dic


从字典中取值,并返回匹配号的列表
[Python] 纯文本查看 复制代码
1
2
3
4
def pipei(shujuyuan, daipipei):
    with contextlib.suppress():
        pipei_list = daipipei.split('\n')
        return [shujuyuan.get(i.strip(), '') for i in pipei_list]


统计数据的行数,默认会去掉数据末的不可见字符
[Python] 纯文本查看 复制代码
1
2
3
def count_items(strs, lclean=False):
    strs = strs.strip() if lclean else strs.rstrip()
    return len(strs.split('\n'))


GUI部分,使用Column来布局,感觉没有Frame好看
[Python] 纯文本查看 复制代码
1
2
3
4
5
6
7
left_col = sg.Column(layout_left, element_justification='center', pad=(5))
mid_col = sg.Column(layout_mid, element_justification='center', pad=(5))
right_col = sg.Column(layout_right, element_justification='center', pad=(5))
bottom_left = sg.Column(layout_bottom_left, element_justification='left')
bottom_right = sg.Column(layout_bottom_right,
                         element_justification='center',
                         pad=(9))


逻辑判断部分,很渣,暂时没有找到更好的处理方法,幸好运行没问题
[Python] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
if event == '粘数据源':
    window['数据源'].update(jtb)
    print(f'已粘贴{count_items(jtb)}条数据')
if event == '清空数据源':
    window['数据源'].update('')
    window['log内容'].update('')
if event == '粘待匹配':
    window['待匹配'].update(jtb)
    print(f'已粘贴{count_items(jtb)}条数据')
if event == '清空待匹配':
    window['待匹配'].update('')
    window['log内容'].update('')
if event == '执行匹配':
    print('执行匹配')
    window['结果'].update('')
    if not values['数据源'] or not values['待匹配']:
        print('数据源或待匹配数据为空,无法执行匹配')
    else:
        with contextlib.suppress():
            shujuyuan = data_source_dic(values['数据源'], values['左到右'])
            if pipei_list := pipei(shujuyuan, values['待匹配']):
                window['结果'].update('\n'.join(pipei_list))
 
if event == '复制结果':
    # print(event, values)
    res = values['结果']
    if res.strip():
        copy(res)
        print('✔ 已复制匹配结果')
    else:
        print('结果为空,没复制任何内容')
if event == '清空结果':
    window['结果'].update('')
    window['log内容'].update('')



完整代码,喜欢的取走,欢迎意见建议,大家一起进步。
[Python] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from pyperclip import copy, paste
import PySimpleGUI as sg
import contextlib
 
 
def data_source_dic(shujuyuan, direction=True, fenge='\t'):
    if not (shujuyuan := shujuyuan.strip()):
        return
    data_source = shujuyuan.split('\n')
    if fenge not in data_source[0]:
        fenge = ' '
    data_dic = {}
    with contextlib.suppress():
        for i in data_source:
            if i:
                fen_list = i.split(fenge)
                if len(fen_list) > 1#处理单列的情况
                    if direction:
                        k, *_, v = fen_list  #左→右
                    else:
                        v, *_, k = fen_list  #右←左
                else:
                    k = fen_list[0]
                    v = ''
                if k not in data_dic:
                    data_dic[k.strip()] = v.strip()
        return data_dic
 
 
def pipei(shujuyuan, daipipei):
    with contextlib.suppress():
        pipei_list = daipipei.split('\n')
        return [shujuyuan.get(i.strip(), '') for i in pipei_list]
 
 
def count_items(strs, lclean=False):
    strs = strs.strip() if lclean else strs.rstrip()
    return len(strs.split('\n'))
 
 
sg.theme('GrayGrayGray'# 设置当前主题
 
#数据源
source_input = sg.Multiline('', key='数据源', size=(35, 20))
source_paste = sg.Button(' 粘数据源 ', key='粘数据源', pad=(8))
source_clean = sg.Button(' 清 空 ', key='清空数据源', pad=(8))
 
#待匹配
pipei_input = sg.Multiline('', key='待匹配', size=(30, 20))
pipei_paste = sg.Button(' 粘待匹配 ', key='粘待匹配', pad=(8))
pipei_clean = sg.Button(' 清 空 ', key='清空待匹配', pad=(8))
 
#结果
result_lookup = sg.Button(' 执行匹配 ',
                          key='执行匹配',
                          button_color='#ffa631',
                          pad=(8))
result_input = sg.Multiline('',
                            key='结果',
                            size=(30, 20),
                            background_color='#f0f0f0',
                            text_color='#ca6924')
result_copy = sg.Button(' 复 制 ', key='复制结果', pad=(8))
result_clean = sg.Button(' 清 空 ', key='清空结果', pad=(8))
 
#高级选项
zuoyou_true = sg.Radio('最左 ▶ 最右 (默认)', '模式', key="左到右", default=True, pad=(3))
zuoyou_false = sg.Radio('最左 ◀ 最右', '模式', key="右到左", pad=(3))
 
log_output = sg.Output(key='log内容',
                       size=(84, 3),
                       background_color='#f0f0f0',
                       text_color='#808080')
 
#放置组件
layout_left = [
    [source_input],
    [source_paste, source_clean],
]
 
layout_mid = [
    [pipei_input],
    [pipei_paste, pipei_clean],
]
 
layout_right = [
    [result_input],
    [result_lookup, result_copy, result_clean],
]
 
layout_bottom_left = [
    [zuoyou_true],
    [zuoyou_false],
]
 
layout_bottom_right = [[log_output]]
 
#将各组件转换为sg的列
left_col = sg.Column(layout_left, element_justification='center', pad=(5))
mid_col = sg.Column(layout_mid, element_justification='center', pad=(5))
right_col = sg.Column(layout_right, element_justification='center', pad=(5))
bottom_left = sg.Column(layout_bottom_left, element_justification='left')
bottom_right = sg.Column(layout_bottom_right,
                         element_justification='center',
                         pad=(9))
 
# 将列布局
layout = [
    [left_col, mid_col, right_col],
    [bottom_left, bottom_right],
]
 
# 创建窗口
window = sg.Window(
    '匹配字符 - by [url=mailto:wkdxz@52pojie.cn]wkdxz@52pojie.cn[/url]',
    layout,
    font=('微软雅黑', 10),
    element_justification='center',
    keep_on_top=True,
    finalize=True,
)
 
while True:
    event, values = window.read()
    if not event:
        break
 
    jtb = paste()
 
    if event == '粘数据源':
        window['数据源'].update(jtb)
        print(f'已粘贴{count_items(jtb)}条数据')
    if event == '清空数据源':
        window['数据源'].update('')
        window['log内容'].update('')
    if event == '粘待匹配':
        window['待匹配'].update(jtb)
        print(f'已粘贴{count_items(jtb)}条数据')
    if event == '清空待匹配':
        window['待匹配'].update('')
        window['log内容'].update('')
    if event == '执行匹配':
        print('执行匹配')
        window['结果'].update('')
        if not values['数据源'] or not values['待匹配']:
            print('数据源或待匹配数据为空,无法执行匹配')
        else:
            with contextlib.suppress():
                shujuyuan = data_source_dic(values['数据源'], values['左到右'])
                if pipei_list := pipei(shujuyuan, values['待匹配']):
                    window['结果'].update('\n'.join(pipei_list))
 
    if event == '复制结果':
        # print(event, values)
        res = values['结果']
        if res.strip():
            copy(res)
            print('✔ 已复制匹配结果')
        else:
            print('结果为空,没复制任何内容')
    if event == '清空结果':
        window['结果'].update('')
        window['log内容'].update('')
 
window.close()

免费评分

参与人数 9吾爱币 +12 热心值 +9 收起 理由
MOMOMOMO + 1 谢谢@Thanks!
wushaominkk + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
一场荒唐半生梦 + 1 + 1 谢谢@Thanks!
Alex_107 + 1 + 1 谢谢@Thanks!
光的意境 + 1 我很赞同!
zz08808 + 1 + 1 热心回复!
虚空先森 + 1 + 1 我很赞同!
双翼 + 1 用心讨论,共获提升!
koongkoong + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| wkdxz 发表于 2022-10-11 21:33
随遇而安8 发表于 2022-10-11 21:04
能不能指定提取多列,等待大佬2.0版本

可以  只是需求量不大 所以没有做
 楼主| wkdxz 发表于 2022-10-11 20:10
xzchina 发表于 2022-10-11 19:32
大佬问下是用什么工具录制的gif动画

不是大佬哈,Gif123论坛有下载的
Cacarot 发表于 2022-10-11 16:31
双翼 发表于 2022-10-11 16:45
这个对我有用,收藏下
kuwu 发表于 2022-10-11 17:15
大佬牛逼 学习一下
ww5270616 发表于 2022-10-11 17:20
这个也不错,感谢分享
ainihd 发表于 2022-10-11 18:04
成品在哪里下载
 楼主| wkdxz 发表于 2022-10-11 18:22
ainihd 发表于 2022-10-11 18:04
成品在哪里下载

顶部有链接
ai28012801 发表于 2022-10-11 18:42
感谢分享
xzchina 发表于 2022-10-11 19:32
大佬问下是用什么工具录制的gif动画
Alex_107 发表于 2022-10-11 19:33
收藏,感谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-4-6 23:57

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表