Python merge text File sample code.
Python implements two text merges
Work number and name are recorded in the employee file
Cat Employee.txt:
Jason Smith
John Doe
Sanjay Gupta
Ashok Sharma
Record work number and salary in bonus file
Cat Bonus.txt:
100 $5,000
200
300 $3,000
400 $1,250
Request to merge two files and output as follows, processing the result:
Ashok Sharma $1,250
Jason Smith $5,000
John Doe
Sanjay Gupta $3,000
This is supposed to be written by the shell, but my shell is not very good, I use Python to achieve
Note that, according to the title, the output file also needs to be sorted by initials.
#! /usr/bin/env Python#coding=utf-8fp01=open ("Bonus.txt", "R") A=[]for Line01 in Fp01:a.append (LINE01) Fp02=open (" Employee.txt "," R ") fc02=sorted (Fp02,key=lambda x:x.split () [1]) for LINE02 in Fc02:i=0while line02.split () [0]!=a[i]. Split () [0]:i+=1print '%s%s%s%s '% (Line02.split () [0],line02.split () [1],line02.split () [2],a[i].split () [1]) Fp01.close () Fp02.close ()
Python merge text File sample code