我有个朋友让我帮她写这个,因为她发现她的日志是用纯文本保存私人银行信息的。这用于解析数百个文本文件。除了宠物项目之外,我从来没有和python合作过,所以我想给我的朋友发送一段很好的代码,因为这是我第一次帮助她。不过,我不想让这个多线程。
对这方面的改进或经验教训有何建议?提前谢谢你!
import os
scan1_location=6
scan2_location=3
scan3_location_a=38
scan3_location_b=60
scan1="bankAccount: "
scan2="bankAccount,"
scan3=" accountNumber="
input_directory="/home/ubuntu/CCTech_Dev/Stories/S-68080/logFiles/"
output_directory="/home/ubuntu/CCTech_Dev/Stories/S-68080/newLogs/"
found=0
count=0
# 1. Parse the given line for the number to be masked
# 2. Mask the number
# 3. Insert masked number back into line
def maskLine(line, scan):
# Depending on the scan, the number is in a different location in the line
if scan == 1:
number = line[scan1_location]
number = maskNumber(number)
line[scan1_location] = number
elif scan == 2:
subLine = line[scan2_location].split(",")
subLine[3] = maskNumber(subLine[3])
line[scan2_location] = ",".join(subLine)
elif scan == 3:
# The number in this scan can either occur once in a short line
# or twice in a long line.
if len(line) < 50:
subLine = line[len(line) - 4].split("=")
subLine[1] = maskNumber(subLine[1])
line[len(line) - 4] = "=".join(subLine)
else:
subLine = line[scan3_location_a].split("=")
subLine[1] = maskNumber(subLine[1])
line[scan3_location_a] = "=".join(subLine)
line[scan3_location_b] = "=".join(subLine)
else:
raise Exception("Location Error")
return " ".join(line)
# Example 1: Input -> 123, Output -> *****
# Example 2: Input -> 123456789, Output -> *****6789
def maskNumber(number):
if len(number) > 0 and len(number) < 5:
number = "*****"
else:
number = "*" * (len(number) - 4) + number[-4:]
return number
# Run through each line of every file to mask every bank account number found
# Iterate over every file in directory
for filename in os.listdir(input_directory):
print(input_directory + filename)
# Output file may be in the same location as input depending on
# output_directory but will always have a different file name
file = open(input_directory + filename, 'r')
output = open(output_directory + filename + "-new", 'w+')
# Initial read of file
line = file.readline()
length = len(line)
# Read until the end of the file
# Ensures any erroneous reads are not parsed
while length > 0:
# Check if line contains a number that needs to be masked
size = line.find(scan1, 0, length)
location=scan1_location
scan=1
if size == -1:
size = line.find(scan2, 0, length)
scan=2
if size == -1:
size = line.find(scan3, 0, length)
scan=3
if size > -1:
# Count the number of numbers masked (because I like numbers)
found+=1
line = line.split(" ")
line = maskLine(line, scan)
# Newline is removed during the parsing process
output.write(line + "\n")
else:
output.write(line)
# Count total number of lines (because I like numbers)
count+=1
# Read next line
line = file.readline()
length = len(line)
file.close()
print(count)
print(found)
output.close()更新:根据RootTwo的请求,下面是我将要编辑的几个示例字符串。为了回答他们的问题,在我测试运行的2,000,000+行中,我只捕获了17,000行有保护信息的行。所以它们是非常分散的。但我需要掩盖的所有数字都是在三起案件之后出现的:
scanX_location应该是数字所在的“行”中的静态数组位置,但我最近发现“行”的大小在一些情况下是动态的。因此,我需要重新计算,找到每一行的数字所在。这里有几个例子。
(第五个例子是一个不需要掩蔽的行。)
发布于 2019-09-22 07:57:38
现在,您将使用钩子来维护这段代码(这很好)。所以,把所有的东西都记录得很好。scan1_location是什么意思,为什么它等于6,但是scan2_location = 3。你还记得为什么一年后当你的朋友要求你更新代码来处理一种新的行,或者当其中一种行格式改变的时候吗?
包含一个顶级的docstring,解释文件所做的任何原因。包括每一种线条的样本。
看起来这是一个命令行程序。因此,让它成为一个适当的命令行程序。查看一下“单击”库。至少,它应该有一种方法来获得适当使用程序的帮助。我会添加一种方式来打开日志,帮助跟踪不可避免的错误。
想必,帐号在文件中被蒙蔽了,所以那些不允许查看帐号的人可以看到或使用这些文件。在同一个目录中混合蒙面文件和未隐藏文件是在请求某人意外发送错误的文件。
open()可用作上下文管理器。
如果放错位置,它看起来就像output.close()。每当打开一个新的输入字段时,都会打开一个新的输出文件,它们可能也应该一起关闭。在处理完文件时,确保文件被关闭的最简单方法是使用with语句:
with open(...) as input_file, open(...) as output_file:
... process the files in this block of code ...
... the files automatically get closed here ...要迭代文件中的行,请使用:
for line in input_file:
... process the lines ...start和end默认为0和字符串的长度,因此不需要提供它们。
这就是目前的情况。
https://codereview.stackexchange.com/questions/229439
复制相似问题