Python implements file information merging instance code,
Combine teleaddressbook.txtand emailaddressbook.txtinto a single addressbook.txt
Def main (): ftele1 = open ("d: \ TeleAddressBook.txt", "rb") ftele2 = open ("d: \ EmailAddressBook.txt", "rb") ftele1.readline () ftele2.readline () lines1 = ftele1.readlines () lines2 = ftele2.readlines () # create an empty list to store the name: Email list1_name = [] listdomaintele = [] list2_name = [] list2_email = [] # obtain TeleAddressBook for line in lines1: elements = line. split () listpolicname.append (str (elements [0]. decode ("gbk") listseartele.append (str (elem Ents [1]. decode ("gbk") # obtain EmailAddressBook for line in lines2: elements = line. split () list2_name.append (str (elements [0]. decode ("gbk") list2_email.append (str (elements [1]. decode ("gbk") lines = [] lines. append ("Name \ t phone \ t email \ n") # traverse the name list by index 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], listdomaintele [I], list2_email [j]) S + = "\ n" else: s = "\ t ". join ([list1_name [I], listdomaintele [I], str ("-----------")]) s + = "\ n" lines. append (s) 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) # write the new merged data to the new file. ftele3 = open ("d: \ AddressBook.txt", "w") ftele3.writelines (lines) # Close the ftele3.close () ftele1.close () ftele2.close () print ("The addressBooks are merged! ") Main ()
Demo result:
Summary
The above is all the content of the Python implementation file to merge the instance code. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!