Python implements file information merging instance code,

Source: Internet
Author: User

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!

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.