将列表元素附加到某个txt文件

时间:2017-09-27 12:49:58

标签: python

我有一个用户列表和一个txt文件列表。 对于与用户匹配的每个文件,我需要“打开”匹配的文件,并在字符串“ID NAME:”后面附加用户名。

我的文件是这样的:

j.smith@google.com-20170927.txt
a.cooper@google.com-20170925.txt
s.king@google.com-20170926.txt
...
...

我试试这个但不起作用:

import os
import glob
import re

users = ['j.smith@google.com', 'a.cooper@google.com', 's.king@google.com']

'''Get user without mail ext'''
a = []
for itemc in (users):
    itemc = itemc.rpartition('@')[0]
    a.append(itemc)

'''Get all txt file'''
c = []
txt_file = []
os.chdir("C:\txt")
files = glob.glob('*.txt')
for file in files:
    txt_file.append(file)
c = []

''' Append user to txt file -NOT WORK '''
for itema in users:
for itemb in (txt_file):
    if re.search(itema, itemb):
        c.append(itemb)
        for txtfile in c:
            with open(txtfile, 'rw') as f:
                lines = f.readlines()
            for i, line in enumerate(lines):
                if line.startswith('ID NAME:'):
                    line[i] = line[i].strip() + (itema)
            f.seek(0)
            for line in lines:
                f.write(line)

我的原始文件示例:

A:CALFSL
VERSION:1.0
SCALE:G
B:REQUEST
C:010101010
START:20170929T000000
END:20170929T000000
STAMP:20170927T101000
MAIL:mailto:user@domain.com
CREATED:20170927T101000
DESCRIPTION:USER
CREATED:20170927T101000
SEQUENCE:0
STATUS:OK
ID NAME:
V:000
Z:END

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

node {
  FOO = world
  stage('Preparation') { // for display purposes
      sh "ls -a && pwd && echo ${FOO}"
  }
}