我在 python 中是一个完全的菜鸟:我将如何重构下面的代码,以便字典d实际上是文件系统上的一个文件,如果在 fr 中找到一个新的电子邮件地址,它会通过电子邮件和下一个增量 int id 附加到文件系统上?
这是fr文件的文件结构:
7@comp1.COM|4|11|GDSPV
7@comp1.COM|16|82|GDSPV
13@comp1.COM|16|82|GDSPV
下面是我的程序..它用 id 屏蔽电子邮件地址。请注意,目前,我已经对 d 字典进行了硬编码。
d= {
'7@comp1.COM': '199',
'8@comp4.COM': '200',
'13@comp1.COM': '205'
}
fr = open(sys.argv[1], 'r')
fw = open("masked_"+sys.argv[1], 'w')
cnt = 0
i = 1
line_list = []
for line in fr:
columns = line.split("|")
looking_for = columns[0] # this is what we need to search
if looking_for in d:
# by default, iterating over a dictionary will return keys
new_line = d[looking_for]+'|'+'|'.join(columns[1:])
line_list.append(new_line)
fw.writelines(line_list)
fr.close()
fw.close()
我也想通过这个程序发送多个文件。其中数百个使用通配符或其他东西 (*) 作为 fr 文件阅读器。您能否将其包含在解决方案中是可能的。谢谢!!!