导入文本文件并将其操作以创建新文件

时间:2014-09-21 04:55:26

标签: python lotus-domino lotus

我正在编写一个脚本,我可以在工作中用于批量Lotus note用户注册。

基本上我需要将包含用户名列表的文本文件操作到我可以导入多米诺管理员的文件中。

例如,我有一个包含

的文本文件
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]

我已经花了好几个小时摆弄这个。任何帮助都会很棒:)

1 个答案:

答案 0 :(得分: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;