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