Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将列表元素附加到某个txt文件中_Python - Fatal编程技术网

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

Python 将列表元素附加到某个txt文件中,python,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@goog

我有一个用户列表和一个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
您可以尝试以下方法:

s = ["j.smith@google.com-20170927.txt", "a.cooper@google.com-20170925.txt", "s.king@google.com-20170926.txt"]
for name, filename in [i.split('-') for i in s]:
    new_file_data = ["ID NAME:"+name[:name.index("@")]+"\n" if i.startswith("ID NAME:") else "MAIL:mailto:"+name + "\n" if i.startswith("MAIL") else i for i in open(name+filename)]
    final_file = open(name+filename, 'w')
    for line in new_file_data:
        final_file.write(line)
    final_file.close()

你的问题是什么?遇到了什么错误/问题?注意:
rw
模式不存在。您需要
r+
。这可能就是我在这个示例中看到的问题:。但我不知道如何处理一个列表,而不是一个文件或“用户”,我尝试使用“r+”但不起作用。我得到以下错误:IOError:[Errno 2]没有这样的文件或目录:'20170927.txt'@sudosu请现在再试一次。我修改了密码,他搜索“j。smith@google.com20170927.txt“而不是”j。smith@google.com-20170927.txt“我将此添加到脚本”-“新的\u文件\u数据=[I.strip('\n')+name if I.strip('\n')==”ID name:“else I for I in open(name+'-'+filename)],但此脚本不会在“ID name:”之后添加“用户名”:@sudosu请发布一个你的原始文件的样子和最终文件应该是白色的例子。谢谢你的耐心。请看这里我想要的: