首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析大型文本文件-掩蔽所有银行编号

问解析大型文本文件-掩蔽所有银行编号
EN

Code Review用户
提问于 2019-09-22 02:52:22
回答 1查看 514关注 0票数 3

我有个朋友让我帮她写这个,因为她发现她的日志是用纯文本保存私人银行信息的。这用于解析数百个文本文件。除了宠物项目之外,我从来没有和python合作过,所以我想给我的朋友发送一段很好的代码,因为这是我第一次帮助她。不过,我不想让这个多线程。

对这方面的改进或经验教训有何建议?提前谢谢你!

代码语言:javascript
复制
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行有保护信息的行。所以它们是非常分散的。但我需要掩盖的所有数字都是在三起案件之后出现的:

  1. "bankAccount:“
  2. ",bankAccount,“
  3. “accountNumber=”

scanX_location应该是数字所在的“行”中的静态数组位置,但我最近发现“行”的大小在一些情况下是动态的。因此,我需要重新计算,找到每一行的数字所在。这里有几个例子。

(第五个例子是一个不需要掩蔽的行。)

  1. 11.11.11.11.1111111111111.111111.IVR,09/16/2019 00:46:24.190,进行支票付款,自定义,输入- bankAccount: 111111111111 routingNumber: 111111111
  2. 11.11.11.11.1111111111111.111111.IVR,09/16/2019 00:49:54.992,进行支票付款,定制,bankAccount,*3060
  3. 11.11.11.11.111111111111.111111.IVR,09/16/2019 00:49:54.992,核对付款、海关、ivrUser、IvrUser{environment=prod accountId=111111111 primaryPerson=1111111111 personIdList= houseNumber=11111 streetName=N主St N单元11 zipCode=11111 phoneNumber=1111111111 phoneNumberFormatted=(111) 111-1111 customerClass=Normal numberOfAccounts=11 accountBalance=11.11 accountDueDate=2019年-09-10 isEligible=true isEligibleReason=null accountBalance=11.11{en19#最新付款日期=2019-08-15}en21 en23 en25#{en29#en29}en34 31 en34 34#en35#en36}
  4. 11.11.11.11.1111111111111.111111.IVR,09/16/2019 18:37:35.857,为: Person{personId=1111111111 personOrBusiness=P version=11 paymentOptionId=null bankInfoList=}检查付款、自定义、FYI、开始删除保存的支付信息。
  5. 11.11.11.11.1111111111111.111111.IVR,09/16/2019 18:37:36.904,为: Person{personId=1111111111 personOrBusiness=P version=3 paymentOptionId=null bankInfoList=[]]制作支票付款、自定义、FYI、开始删除保存的支付信息。
EN

回答 1

Code Review用户

回答已采纳

发布于 2019-09-22 07:57:38

文档所有的东西

现在,您将使用钩子来维护这段代码(这很好)。所以,把所有的东西都记录得很好。scan1_location是什么意思,为什么它等于6,但是scan2_location = 3。你还记得为什么一年后当你的朋友要求你更新代码来处理一种新的行,或者当其中一种行格式改变的时候吗?

包含一个顶级的docstring,解释文件所做的任何原因。包括每一种线条的样本。

专用命令行程序

看起来这是一个命令行程序。因此,让它成为一个适当的命令行程序。查看一下“单击”库。至少,它应该有一种方法来获得适当使用程序的帮助。我会添加一种方式来打开日志,帮助跟踪不可避免的错误。

独立输出目录

想必,帐号在文件中被蒙蔽了,所以那些不允许查看帐号的人可以看到或使用这些文件。在同一个目录中混合蒙面文件和未隐藏文件是在请求某人意外发送错误的文件。

open()可用作上下文管理器

。

如果放错位置,它看起来就像output.close()。每当打开一个新的输入字段时,都会打开一个新的输出文件,它们可能也应该一起关闭。在处理完文件时,确保文件被关闭的最简单方法是使用with语句:

代码语言:javascript
复制
with open(...) as input_file, open(...) as output_file:

    ... process the files in this block of code ...

... the files automatically get closed here ...

打开的文件是可迭代的

要迭代文件中的行,请使用:

代码语言:javascript
复制
for line in input_file:
    ... process the lines ...

str.find(子字符串,开始,结束)

start和end默认为0和字符串的长度,因此不需要提供它们。

这就是目前的情况。

票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/229439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档