Python3 merging two files to form a contacts

Source: Internet
Author: User

Learn the code of others and learn the Python syntax

[email protected] student]# cat TeleAddressBook.txt

Name Phone

Lee 413,567,428,765

Zhang 318,878,972,314

Wang 518,273,719,921

Chen 619,903,210,032

[email protected] student]# cat EmailAddressBook.txt

Name Email

Zhang San [email protected]

John Doe [email protected]

Harry [email protected]

Hello [email protected]

[email protected] student]# cat AddressBook.txt

Name phone email

John Doe 13567428765[email protected]

Zhang San 18878972314[email protected]

Harry 18273719921[email protected]

Chen Liu 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 ') #去掉文本标题行, that is, skip the first line >>> ftele1.readline () ' name   phone \ n ' #readlines循环读取每一行, make up a list # here remember: If there is a blank line in the text, there will be ' \ n ' &nbsp in the Lines1 list. The following list append the error >>> lines1  = ftele1.readlines () >>> lines1[' John Doe  13567428765\n ',  ' Zhang San  18878972314\n ',  ' Harry  18273719921\n ',  ' Chen Liu  19903210032\n ']>>> >>> lines1[0] ' John Doe   13567428765\n ' #通过对字符串切片形成列表, Value >>> lines1[0].split () [' John Doe ',  ' 13567428765 ']>>>  Elements = lines1[0].split () >>> elements[0] ' John Doe ' List1_name = []list1_tele  = []list2_name&nbsP;= []list2_email = [] #获取第一个文本中的姓名和电话信息for  line in lines1:elements =  Line.split () #此处列表添加有个隐患: The text file must not have a blank line, responsible for append will be error #indexerror: list index out of  Rangelist1_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 (' name \t  phone \t  mailbox \t \n ') #遍历列表匹配一样姓名的用户, construct string 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 ()


This article is from the "unplug the Operational Space" blog, please be sure to keep this source http://zhangdj.blog.51cto.com/9210512/1882949

Python3 merging two files to form a contacts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.