2015-04-05 48 views
-2

我有如下2個.txt文件:我怎樣才能合併2個.txt文件?

letter.txt:

[fname] [lname] 
[street] 
[city] 

Dear [fname]: 

    As a fellow citizen of [city], you and all your neighbours 
on [street] are invited to a celebration this Saturday at 
[city]'s Central Park. Bring beer and food! 

q2.txt:

Michael 

dawn 

lock hart ln 

Dublin 

-- 

kate 

Nan 

webster st 

king city 

-- 

raj 

zakjg 

late Road 

Toronto 

-- 

dave 

porter 

Rock Ave 

nobleton 

-- 

John 

Doe 

round road 

schomberg 

如何合併產生的文件和打印個性化的字母例如第一個地址應該打印:

邁克爾

黎明

鎖HART LN

都柏林

親愛的邁克爾:

都柏林的同胞,你和你的鄰居 上鎖定HART LN被邀請到一個慶祝這個星期六 都柏林中央公園。帶上啤酒和食物!

結論:我怎樣才能創建一個函數來合併這2個.txt文件,使個性化的信嗎?

我有什麼至今:

first_file = open("letter.txt", "r") 

dest_file = open("q2.txt", 'w') 

for line in first_file: 

    v=line.split() 

    for x in v: 

     if x[0]=="fname": 

      dest_file.write(x+"\n") 


first_file.close() 

dest_file.close() 
+0

這肯定是可能的。請告訴我們你到目前爲止有什麼。 – fuesika 2015-04-05 21:36:41

+0

如果你願意稍微改變letterFile.txt的格式,你可以使用它作爲[Jinja2的(http://jinja.pocoo.org/docs/dev/)模塊的模板。 – alexwlchan 2015-04-05 21:37:01

+0

見[字符串格式化](https://docs.python.org/2/library/string.html#formatstrings),尤其是[實施例](https://docs.python.org/2/library/string。 HTML格式#-例子)。 – 2015-04-05 21:43:38

回答

4

一旦你已經發現瞭如何從你的第二個文件讀取的變量,你可以通過多種方式代替它們在你的模板。最簡單的方法是使用帶變量的.format() method。在模板中,您可以通過在.format()方法中添加{fname}並將它們添加爲變量來定義標籤。

"""{fname} {lname} 
{street} 
{city} 

Dear {fname}, 

    As a fellow citizen of {city}, you and all your neighbours 
on {street} are invited to a celebration this Saturday at 
{city}'s Central Park. Bring beer and food!""".format(fname='John', lname='Doe', street='Main St', city='Anywhere') 

輸出:

John Doe 
Main St 
Anywhere 

Dear John, 

    As a fellow citizen of Anywhere, you and all your neighbours 
on Main St are invited to a celebration this Saturday at 
Anywhere's Central Park. Bring beer and food! 
+0

我想要它,所以我的功能正在做所有的工作。我需要它來查找fname和lname ect,並用地址文件替換它們。 – stacker 2015-04-05 22:37:34

+0

我知道你想要那個,但是你應該把這個方法和你的文件結合起來。你的第一個文件可以用'open()'簡單地讀取,你應該爲第二個文件編寫一個解析器,以便它返回一個字典或一個變量列表。然後你可以將它組合成一個產生輸出的函數。 – erikgaal 2015-04-05 23:07:56

+0

不完整,但使用格式的好主意。 – Blue 2015-04-05 23:13:18

1

儘量this.I認爲最醜的way.Please等待好的答案。

with open('AdressFile.txt') as f: #assuming as a large file 
    for i in f: 
     fname = i 
     next(f,None) #skipping \n 
     lname = next(f,None) 
     next(f,None) #skipping \n 
     street = next(f,None) 
     next(f,None) #skipping \n 
     city = next(f,None) 
     next(f,None) #skipping \n 
     next(f,None) #skipping ----- 
     next(f,None) #skipping \n 

     with open('letterFile.txt') as f1: 
      temp = f1.read() # assuming as a small file 
      temp = temp.replace('[fname]',fname.strip()) 
      temp = temp.replace('[lname]',lname.strip()) 
      temp = temp.replace('[city]',city.strip()) 
      temp = temp.replace('[street]',street.strip()) 
     print(temp) 

#output 
Michael dawn 
lock hart ln 
Dublin 

Dear Michael: 

    As a fellow citizen of Dublin, you and all your neighbours 
on lock hart ln are invited to a celebration this Saturday at 
Dublin's Central Park. Bring beer and food! 

...... 
2

閱讀文件:

letter = '' 
q2 = '' 

with open('letter.txt', 'r') as f: 
    letter = f.read() 
f.close() 

with open('q2.txt', 'r') as f: 
    q2 = f.read() 
f.close() 

然後定義一些功能:

def cleanData(query): 
return [item.strip().split('\n\n') for item in query.split('--')] 

def writeLetter(template, variables, replacements): 
    # replace ith variable with ith replacement variable 
    for i in range(len(variables)): 
     template = template.replace(variables[i], replacements[i]) 
    return template 

然後:

variables = ['[fname]', '[lname]', '[street]', '[city]'] 
letters = [writeLetter(letter, variables, person) for person in cleanData(q2)] 

這裏有一個[編輯:更新] IPython中的notebook它。

+0

我可以做到這一點,而不包括我的功能模塊中的文件? @metasyn – stacker 2015-04-05 22:51:18

+0

是的,你可以單獨閱讀它們,因爲我更新了顯示的答案。 – metasyn 2015-04-06 23:33:28