解析大型excel文件并将结果写回到带分隔符的文本文件中

时间:2019-06-14 15:07:02

标签: python excel python-3.x parsing

注意:由于询问“代码审查”该怎么办?问题不在我的讨论之列,所以我将在这里尝试。

我在一家跨国公司担任IT实习生,完成了一项繁琐的任务,即通过2500多个长的多列excel报告文件进行组合以查找不活动的服务器。

以下是该文件中的示例行:

enter image description here

然后,我得到了另一个excel文件,但是这次仅包含DB代码(其中80多个)。

我的任务是:

  • 浏览大报告文件
  • 通过数据库代码查找公司
  • 检查服务器是否处于活动状态,如果未处于活动状态,则将其标记为停用

当然,正如您所期望的,我被告知以以下格式在电子表格中返回结果:

Full name: Acme Inc. | Code: ACM | Active?: no | Decomm?: yes

Fulln name:, Code:, etc.是列标题。在这里,它们只是为了提高可读性。

如果我要手动进行操作,很可能会因为无聊而死。但!有Python,对吗?

因此,我将报告中的某些列导出到制表符分隔的文件中,并草拟了以下内容:

def read_file_to_list(file_name):
    with open(file_name, 'r') as file_handler:
        stuff = file_handler.readlines()
    return [line[:-1] for line in stuff]


def make_dic(file_name):
    with open(file_name, 'r') as f:
        rows = (line.replace('"', "").strip().split("\t") for line in f)
        return {row[0]:row[1:] for row in rows}


def search(dic, ou_codes):
    c = 1
    for k, v in dic.items():
        for code in ou_codes:
            if v[0] == code:
                print("{}. Full name: {} | Code: {} | Active?: {} | Decomm?: {}".format(c, k, *v, "yes" if v[1] == "no" else "no"))
                c += 1


decomm_codes = read_file_to_list('decomm_codes.txt')
all_of_it = make_dic('big_report.txt')

search(all_of_it, decomm_codes)

那会吐出来:

1. Full name: Random, Inc | Code: RNDM | Active?: yes | Decomm?: no
2. Full name: Acme Inc.| Code: ACM | Active?: no | Decomm?: yes
3. Full name: Fake Bank, Ltd.  | Code: FKBNK | Active?: yes | Decomm?: no

问题:

在终端窗口中看起来一切都很好,但是如何将结果写回到制表符分隔的文本文件中?所以看起来像这样:

Acme Inc. ACM no yes

出于好奇,还有一种方法可以重构search方法,例如一线吗?嵌套列表的理解仍然在我的TO-LEARN列表中(双关语意味深长)。

最后,这是decomm_codes.txtbig_report.txt文件的内容。

decomm_codes.txt:

RNDM
ACM
FKBNK

big_report.txt:

"Random, Inc"   RNDM    yes
Acme Inc.   ACM no
"Fake Bank, Ltd. "  FKBNK   yes

1 个答案:

答案 0 :(得分:1)

您还可以简单地将它们写入文件:

def search(dic, ou_codes):
    c = 1
    # open a file to write to
    with open ("output.tsv","w") as outfile:
        outfile.write( "#\tFull name\tCode\tActive\tDecomm\n")
        for k, v in dic.items():
            for code in ou_codes:
                if v[0] == code:
                    # create output line
                    outputline = "{}\t{}\t{}\t{}\t{}\n".format(
                                 c, k, *v, "yes" if v[1] == "no" else "no")
                    c += 1
                    outfile.write(outputline)
                    print("{}. Full name: {} | Code: {} | Active?: {} | Decomm?: {}".format(
                          c, k, *v, "yes" if v[1] == "no" else "no"))