2014-09-21 71 views
-1

我正在編寫一個腳本,可以在工作中使用批量Lotus lotus用戶註冊。導入文本文件並對其進行操作以創建新文件

基本上我需要操縱一個文本文件,其中包含用戶名列表到一個文件中,我可以導入到多米諾骨牌管理員。

比如我有一個包含一個文本文件,

Gordan.Freeman 
Gordan.Freeman1 
Gordan.Freeman2 

,我需要得到它看起來像

Freeman;Gordan;;;12345678;D:\lotus\NotesIDs\Users\;GordanFreeman.id;SERVER01;mail\users\;GordanFreeman.nsf;;;;;;;;;;template.ntf; 
Freeman1;Gordan;;;12345678;D:\lotus\NotesIDs\Users\;GordanFreeman1.id;SERVER01;mail\users\;GordanFreeman1.nsf;;;;;;;;;;template.ntf; 
Freeman2;Gordan;;;12345678;D:\lotus\NotesIDs\Users\;GordanFreeman2.id;SERVER01;mail\users\;GordanFreeman2.nsf;;;;;;;;;;template.ntf; 

我只是儘可能得到從文本文件讀入一個列表,但從我可以告訴我需要將它轉換回字符串之前,我可以寫入一個新的文本文件。

textloc = input(r" Enter the file path of your list (eg.'C:\names_list.txt) --> ") 
    textopen = open(textloc, 'r') 
    nameslistraw = textopen.read().split('\n') 
    nameslist = [i.split('.') for i in nameslistraw] 

我一直在擺弄這個小時。任何幫助將是偉大的:)

+0

歡迎堆棧溢出!告訴人們[你試過的東西](http://whathaveyoutried.com/)總是很重要的,包括任何失敗嘗試的片段,以便他們能夠理解你錯過了什麼途徑。這很重要,因爲它激勵人們回答問題,這很重要,因爲它可以讓問題更容易提供高質量的相關答案。就目前的問題來看,這尚未實現。如果您編輯問題,可能會阻止問題被關閉,您獲得的答案的數量,質量和清晰度也會提高。 – Veedrac 2014-09-21 04:57:22

+0

@Aidan,盡我們最大的尊重,你不要混淆「任何幫助」和「請爲我做」嗎?顯示你的代碼,並告訴,哪些部分不按預期工作。 – 2014-09-21 05:01:11

+0

添加了我到目前爲止的幾行。真的只需要一個正確的方向。 – Aidan 2014-09-21 05:04:24

回答

0

這是一個工作腳本,做你看起來想要的。

file = open('myfile.txt', 'r') 
temp = [] 
for line in file: 
    item = line.strip('\n').split('.') 
    temp.append(';'.join(item[::-1])+';'*3+'12345678;D:\lotus\NotesIDs\Users\;'+''.join(item)+'.id;SERVER01;mail\users\;'+''.join(item)+'.nsf;;;;;;;;;;template.ntf;') 

file.close() 

file = open('myfile.txt', 'w') 

file.write('\n'.join(temp)) 

file.close() 

,其將執行以下操作:

Gordan.Freeman 
Gordan.Freeman1 
Gordan.Freeman2 

分爲:

Freeman;Gordan;;;12345678;D:\lotus\NotesIDs\Users\;GordanFreeman.id;SERVER01;mail\users\;GordanFreeman.nsf;;;;;;;;;;template.ntf; 
Freeman1;Gordan;;;12345678;D:\lotus\NotesIDs\Users\;GordanFreeman1.id;SERVER01;mail\users\;GordanFreeman1.nsf;;;;;;;;;;template.ntf; 
Freeman2;Gordan;;;12345678;D:\lotus\NotesIDs\Users\;GordanFreeman2.id;SERVER01;mail\users\;GordanFreeman2.nsf;;;;;;;;;;template.ntf; 
+0

這正是我所追求的。雖然遇到了一些有趣的錯誤。我不斷收到一個unicode錯誤「格式錯誤\ N字符轉義」。我用r前綴所有的字符串,現在它可以用來處理。謝謝 – Aidan 2014-09-21 06:34:00

+1

我建議你使用'with'語句來代替打開和關閉。它通常是優選的和更pythonic。 – 2015-07-03 08:40:00

相關問題