python3合并两个文件形成通讯录

学习别人的代码,学习python语法

[[email protected] student]# cat TeleAddressBook.txt

姓名 电话

李四 13567428765

张三 18878972314

王五 18273719921

陈六 19903210032

[[email protected] student]# cat EmailAddressBook.txt

姓名 邮箱

张三 [email protected]

李四 [email protected]

王五 [email protected]

hello [email protected]

[[email protected] student]# cat AddressBook.txt

姓名 电话 邮箱

李四13567428765[email protected]

张三18878972314[email protected]

王五18273719921[email protected]

陈六19903210032------------

hello------------[email protected]

#!/usr/bin/env python3
#-*- coding:utf-8 -*-

def main():
	ftele1 = open("TeleAddressBook.txt",‘r‘)
	ftele2 = open("EmailAddressBook.txt",‘r‘)
	ftele1.readline()
	ftele2.readline()
	lines1 = ftele1.readlines()
	lines2 = ftele2.readlines()
‘‘‘
#读取文件
>>> ftele1 = open("TeleAddressBook.txt",‘r‘)
#去掉文本标题行,也就是跳过第一行
>>> ftele1.readline()
‘姓名 电话\n‘
#readlines循环读取每一行,构成一个列表
#此处切记:如果文本里面有空行,lines1列表中就会有‘\n‘ .后面列表append的时候就会报错
>>> lines1 = ftele1.readlines()
>>> lines1
[‘李四 13567428765\n‘, ‘张三 18878972314\n‘, ‘王五 18273719921\n‘, ‘陈六 19903210032\n‘]
>>> 
>>> lines1[0]
‘李四 13567428765\n‘
#通过对字符串切片形成列表,取值
>>> lines1[0].split()
[‘李四‘, ‘13567428765‘]
>>> elements = lines1[0].split()
>>> elements[0]
‘李四‘
‘‘‘
	list1_name = []
	list1_tele = []
	list2_name = []
	list2_email = []
#获取第一个文本中的姓名和电话信息
	for line in lines1:
		elements = line.split()
#此处列表添加有个隐患:文本文件中一定不能有空行,负责append会报错
#IndexError: list index out of range
		list1_name.append(str(elements[0]))
		list1_tele.append(str(elements[1]))
	for line in lines2:
		elements = line.split()
		list2_name.append(str(elements[0]))
		list2_email.append(str(elements[1]))
	lines = []
	lines.append(‘姓名\t 电话\t 邮箱\t \n‘)
#遍历列表匹配一样姓名的用户,构造字符串
	for i in range (len(list1_name)):
		s = ‘‘
		if list1_name[i] in list2_name:
			j = list2_name.index(list1_name[i])
			s = ‘\t‘.join([list1_name[i],list1_tele[i],list2_email[j]])
			s += ‘\n‘
		else:
			s = ‘\t‘.join([list1_name[i],list1_tele[i],str(‘------------‘)])
			s += ‘\n‘
		lines.append(s)
#处理2中剩余的用户
	for i in range(len(list2_name)):
		s = ‘‘
		if list2_name[i] not in list1_name:
			s = ‘\t‘.join([list2_name[i],str(‘------------‘),list2_email[i]])
			s += ‘\n‘
		lines.append(s)
#写入文件
	ftele3 = open(‘AddressBook.txt‘,‘w‘)
	ftele3.writelines(lines)
	ftele3.close()
	ftele1.close()
	ftele2.close()
	print("game over")
if __name__ == ‘__main__‘:
	main()
时间: 2024-08-12 08:28:05

python3合并两个文件形成通讯录的相关文章

合并两个文件

原文:合并两个文件 源代码下载地址:http://www.zuidaima.com/share/1550463699438592.htm 使用java合并两个已存在的文件的内容到一个新的文件去 import java.io.*; package com.zuidaima.file.util; /** *@author www.zuidaima.com **/ class combinefile{ public static void main(String[] args) throws IOEx

awk合并两个文件

awk 合并两个文件: awk 'NR==FNR{a[i]=$0;i++}NR>FNR{print a[j]"    "$0;j++}' template interface > interface_last template: Template Log tqt_url-response-code Template Log tqt_url-response-code Template Log tqt_url-response-code Template Log tqt_ur

Shell合并两个文件成一个文件的两列

Shell合并两个文件成一个文件的两列 发布时间:2014-07-20   编辑:www.jquerycn.cn Shell合并两个文件成一个文件的两列,提供了两种方法,普通shell脚本,awk脚本. 文件内容如下:more eng.txt chi.txt ::::::::::::::eng.txt::::::::::::::semicoloncommadelimiterspacebarhyphensingle quotedouble quote ::::::::::::::chi.txt::

合并两个文件内容等相关操作

A 两个文件的交集,并集    前提条件:每个文件中不得有重复行1. 取出两个文件的并集(重复的行只保留一份)cat file1 file2 | sort | uniq > file32. 取出两个文件的交集(只留下同时存在于两个文件中的文件)cat file1 file2 | sort | uniq -d > file33. 删除交集,留下其他的行cat file1 file2 | sort | uniq -u > file3B 两个文件合并一个文件在上,一个文件在下cat file1

linux下合并两个文件夹

Linux下目录的合并以及文件的覆盖案例: 有两个目录test和new,test目录下有目录和文件,new目录下有更改过的一些test下的目录和文件,以及一些新增的文件,现在对两个目录进行合并以及覆盖test下的旧文件 cp -frap new/* test/ 命令其实非常简单,解释下: -f  强制覆盖,不询问yes/no(-i的默认的,即默认为交互模式,询问是否覆盖) -r  递归复制,包含目录 -a  做一个备份,这里可以不用这个参数,我们可以先备份整个test目录 -p  保持新文件的属

合并两个文件,替换文件,获取文件大小

合并文件 void OpMergerFile(string readpath, string writepath) { // char cline; // ifstream ifile; // ofstream ofile; // if (_access(readpath.c_str(), 0) == 0) { //#ifdef WIN32 // ifile.open(readpath, ios::binary | ios::in); //读写都要以二进制形式,否则遇到/0就认为结束 // of

利用python合并两个文件

1格式如下 在做利用zabbix的api来批量添加主机的时候,需要处理ip和hostname,在借用别人写的py程序的基础上,自己有改装了以下脚本,为自己使用.需要时ip和hostname为一个统一格式. $ cat ip.txt 1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4 $ cat hostname.txt tx-1 tx-2 tx-3 tx-4 最后需要合并为如下格式 1 tx-1,1.1.1.1 2 tx-2,2.2.2.2 3 tx-3,3.3.3.3 4 tx-4

合并两个文件中,相同的内容

思路: 1.先写一个函数function(line,file):判断line是否在file中. 2.将一个文件的行循环运行上面的函数. def find_line(line,file): f=file.readline() while True: if not f: break if f==line: return line else: return f=file.readline() f_a=open("a.txt") f_b=open("b.txt") f_c=

paste:linux合并两个文件中的列(左右合并)

[[email protected] ~]# paste [-d] file1 file2 选项与参数: -d  :后面可以接分隔字符.默认是以 [tab] 来分隔的! -   :如果 file 部分写成 - ,表示来自 standard input 的数据的意思. 范例一:将 /etc/passwd 与 /etc/shadow 同一行贴在一起 [[email protected] ~]# paste /etc/passwd /etc/shadow bin:x:1:1:bin:/bin:/sbi