Python merges txt data (the first is a list method and the second is a dictionary method)

The python3.9 version of the merged txt file data
I use the centos host of Linux, because it is not convenient to uninstall the built-in pytho2.7, so the python3.9 version is installed, and the path corresponding to python3 is set to python3.9 version.

Insert picture description hereThe first method list
execution command ispython3 tset.py

The contents of test.py

'''第一步——打开文件'''
open1 = open('book1.txt', 'rb')  # 打开姓名电话记录
open2 = open('book2.txt', 'rb')  # 打开姓名邮箱记录
# 采用rb模式打开文本,因为文本里面存在中文,目的避免出现乱码
'''第二步——读取文件 跳过第一行'''
open1.readline()
open2.readline()
'''第三步——读取文件'''
lines1 = open1.readlines()
lines2 = open2.readlines()
''' 第四步——建立空列表 ——————存储姓名,电话,Emial'''
list1_name = []
list1_tele = []
list2_name = []
list2_email = []
# 文本1中读取的每行中  使用tab符号作为切割符
# 第一个name增加的为下标为0的字符串,编码格式为utf-8
# 第二个tele增加的为下标为1的字符串
'''第五步   分割第一份文档里面的内容book1.txt'''
for line in lines1:  # 获取第一个文本中的姓名和电话信息
    elements = line.split()  # 临时列表elements储存 split()方法以(tab符号)分割 姓名与电话
    list1_name.append(str(elements[0].decode('UTF-8')))  # 因为存在中文,解码  格式为utf-8
    list1_tele.append(str(elements[1].decode('UTF-8')))  # 因为存在中文,解码 格式为utf-8
# 文本2的中的每行中 使用tab符号作为切割符
# name下标为0的字符串
# email下标为1的字符串
'''第六步   分割第二份文档的内容book2.txt'''
for line in lines2:
    elements = line.split()
    list2_name.append(str(elements[0].decode('UTF-8')))
    list2_email.append(str(elements[1].decode('UTF-8')))
'''第七步   创建新的列表存储数据——用于合并的第三份book3.txt'''
# 合并处理 ————生成新的数据 创建lines =[] 空列表存储 ——('姓名\t电话\t邮箱\n')
# \t 表示tab符号
# \n 表示换行符号
lines = []
# 第一行添加的为标题
lines.append('姓名\t电话\t邮箱\n')
'''第八步 判断表1是否与表2相同,若相同则合并'''
# 按索引方式遍里姓名列表1
for i in range(len(list1_name)):
    s = ''
    if list1_name[i] in list2_name:  # 判断list1_name[i]元素的每一项是否存在list2_name中
        j = list2_name.index(list1_name[i])  # 使用index方法 找到表2中姓名与表1中姓名的 索引位置
        s = '\t'.join([list1_name[i], list1_tele[i], list2_email[j]])  # 使用join方法合并到临时变量s中
        # 表1的名字,表1的电话,表2对应的邮箱
        s += '\n'# ——————这个是给每行最后添加换行符号
    else:  # 如果未出现则仅仅存储列表1的姓名电话,对于缺失的邮箱则用------符号填充
        s = '\t'.join([list1_name[i], list1_tele[i], str('   -----    ')]) # 使用变量s存储————注意这里使用的是列表方式添加数据
        s += '\n'#  ——————这个是给每行最后添加换行符号
    lines.append(s)  # 最后把s变量存储到lines列表中
'''第九步   处理剩余表2中步再表1的数据'''
# 处理姓名列表2中剩余的姓名
for i in range(len(list2_name)):
    s = ''
    if list2_name[i] not in list1_name:  # 判断表2不在表1中的name
        s = '\t'.join([list2_name[i], str('-------'), list2_email[i]])  # 使用变量s存储————注意这里使用的是列表方式添加数据
        s += '\n'#  ——————这个是给每行最后添加换行符号
    lines.append(s)  # 最后把s变量存储到lines列表中
'''第十步   打开新文件并写入上方的数据'''
open3 = open('book3.txt', 'w')  # 打开并写入新的文档
open3.writelines(lines)  # 使用writelines方法把生成的lines数据存储到ftele3中
'''第十一步 关闭所有文件'''
open3.close()
open2.close()
open1.close()
'''第十二步 设置 操作完毕的数据显示'''
print('操作完毕')

The second method dictionary
executes python3 tset7.py
test7.pythe code of the command as a file

# 使用字典方法合并txt文件
# 1打开文件
openbook1 = open('book1.txt','rb')
openbook2 = open('book2.txt','rb')
# 因为第一行都是信息标题,跳过第一行
# 2处理第一行标题信息
openbook1.readline()
openbook2.readline()
# 3读取剩余的所有行
lines1 = openbook1.readlines()
lines2 = openbook2.readlines()
# 4创建两个空字典
dict1 ={
    
    }
dict2={
    
    }
# 5 把每行的信息作为字典的键和值对存储
# 遍历的每一行 我们得到两个元素 [0]元素为姓名 [1]元素为电话
for line in lines1:
    elements = line.split()
    # 向字典中添加值的操作
    # 把文本读出来的bytes转换为str类型
    # key=elements[0]为名字  value=elements[1]为电话
    dict1[elements[0]] = str(elements[1].decode('UTF-8'))
# 遍历的每一行 我们得到两个元素 [0]元素为姓名 [1]元素为邮箱
for line in lines2:
    elements = line.split()
    # key=elements[0]为名字  value=elements[1]为邮箱
    dict2[elements[0]] =str(elements[1].decode('UTF-8'))
# 6建立新的变量列表储存合并
lines =[]
lines.append("姓名\t 电话\t 邮箱\n")# 第一行为标题信息
# 7合并处理字典1和字典2,并把他们添加到变量lines中
for key in dict1:# 遍历字典1中的key
    s=''
    if key in dict2.keys():
        # print(key.decode('UTF-8')) #键值对中的键=某个名字
        # print(dict1[key])#对应名字的电话
        # print(dict2[key])#对应的名字的邮箱
        s='\t'.join([str(key.decode('UTF-8')),dict1[key],dict2[key] ])
        s+='\n'
        # print(s)#dict11和dict2存在相同名字的数据
    else:
        s='\t'.join([str(key.decode("UTF-8")),dict1[key],str("    -----    ")])
        s+='\n'
    lines.append(s)

for key in dict2:
    s=''
    if key not in dict1.keys():#当dict2中的这个键不在dict1中
        s= '\t'.join( [str(key.decode('UTF-8')), str('    -----    '), dict2[key]])
        # print(key.decode('UTF-8'))#键值对中的键=某个名字
        # print(dict2[key])#键值对的值=邮箱
        s+= '\n'
    lines.append(s)
# 8创建新的文件book3.txtx写入变量中的数据
openbook3 = open('book3.txt','w')
openbook3.writelines(lines)
# 9关闭所有文件
openbook3.close()
openbook2.close()
openbook1.close()
print("处理完毕了")

The combined data list book1.txtdata

姓名	电话
张三	18898836513
李四	18898836514
王五	18898836515
赵六	18898836516
周七	18898836517

The combined data list book2.txtdata

姓名	电话
张三	18898836513@139.com
李四	18898836514@139.com
王五	18898836515@139.com
赵六	18898836516@139.com
林九	18898836519@139.com

Guess you like

Origin blog.csdn.net/weixin_47021806/article/details/113932532