Two texts merged with the Index key association to output unrelated information separately

Source: Internet
Author: User

Original two text
Customer.txt:23115823|3115823|aaaaaa|20030819000000|20040420000000| a| ctc-bj| b23|0| N|0|0
custservice.txt:23115823|fw001| N|1|999912000000|0
after script execution combination: 23115823 ~_~ 23115823 ~_~ aaaaaa ~_~ 20030819000000 ~_~ 20040420000000 ~_~ 0 ~_~ fw001 ~_~ N ~_~ 1 ~_~ 4 ~_~ 0





#Coding=utf-8defcreat_dict (Customer_read_path):
"" "Converts the text to a dictionary, key is the text ID index, and the associated value of two literals; value is the other content" "" with open (Customer_read_path,'R') as Customer:new_dict1= {} forLineinchcustomer.readlines ():ifLine.count ('\ n') ==Len (line): #去除空行Continue Else: LINE_EX= Line.strip ("'). Strip ('\ n'). Split ('|') New_dict1[line_ex[0]]= Line_ex[1:] returnNew_dict1defwrite_dict (Customer_write_path, content): With open (Customer_write_path,'W') as Customer:customer.write (content)defcreat_new_null_list (customer_dict,custservice_dict):
"" Merges two Dictionaries "" by associating the same key customer_new_list=[] customer_null_list= [] forCustomer_keyinchCustomer_dict.keys ():ifCustomer_keyinchCustservice_dict.keys (): Customer_new_list.append ([Customer_key]+ Customer_dict[customer_key] +Custservice_dict[customer_key])Else: Customer_null_list.append (Customer_key)returnCustomer_new_list,customer_null_listcustomer_dict1= Creat_dict ('D:\\360downloads\\customer.txt') Custservice_dict1= Creat_dict ('D:\\360downloads\\custservice.txt')ifLen (customer_dict1)! =Len (custservice_dict1):Print('customer and Custservice does not match') customer_new_list, Customer_null_list=creat_new_null_list (customer_dict1,custservice_dict1) out_lines="'Roamstatus= (LambdaX:'4' ifx = ='0' Else '3' ifx = ='1' Elsex) #用于原始列表中值判断替换为新值fansuan= (LambdaX:'5' ifx = ='Y' Else '0') Custstatus= (LambdaX:'0' ifx = ='A' orx = ='N' Else '1' ifx = ='C' Else '2' ifx = ='O' Else '0') forCuinchcustomer_new_list: #将新list根据转换后组合 cu[+] = Roamstatus (cu[16]) cu[9] = Fansuan (cu[9]) cu[5] = max (Custstatus (cu[5]), Custstatus (cu[13])) Cu_new_list= Cu[:6] + cu[12:15] + [cu[16]] + [cu[9]] Out_lines+='~_~'. Join (Cu_new_list) +'\ n'null_list='\ n'. Join (customer_null_list) write_dict ('D:\\360downloads\\cust_new.txt', Out_lines) write_dict ('D:\\360downloads\\null_new.txt', null_list)

Two texts merged with the Index key association to output unrelated information separately

Related Article

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.