python - how can i merge 2 .txt files? -



python - how can i merge 2 .txt files? -

i have 2 .txt files listed below:

letter.txt:

[fname] [lname] [street] [city] dear [fname]: fellow citizen of [city], , neighbours on [street] invited celebration saturday @ [city]'s central park. bring beer , food!

q2.txt:

michael dawn lock hart ln dublin -- kate nan webster st king city -- raj zakjg late road toronto -- dave porter stone ave nobleton -- john doe round road schomberg

how can merge files produce , print personalized letters illustration first address should print:

michael

dawn

lock hart ln

dublin

dear michael:

as fellow citizen of dublin, , neighbours on lock hart ln invited celebration saturday @ dublin central park. bring beer , food!

in conclusion: how can create function merge these 2 .txt files create personalized letters?

what have far:

first_file = open( "letter.txt", "r") dest_file = open( "q2.txt", 'w') line in first_file: v=line.split() x in v: if x[0]=="fname": dest_file.write(x+"\n") first_file.close() dest_file.close()

once have found out how read variables sec file, can substitute them in template in multiple ways. easiest way this, utilize .format() method variables. in template can define tags adding {fname} , adding them variables in .format() method.

example

"""{fname} {lname} {street} {city} dear {fname}, fellow citizen of {city}, , neighbours on {street} invited celebration saturday @ {city}'s central park. bring beer , food!""".format(fname='john', lname='doe', street='main st', city='anywhere')

output:

john doe main st anywhere dear john, fellow citizen of anywhere, , neighbours on main st invited celebration saturday @ anywhere's central park. bring beer , food!

python python-3.x

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -