【问题标题】:Reading text from multiple html files and consolidate into a different html file python script从多个 html 文件中读取文本并合并到不同的 html 文件中 python 脚本
【发布时间】:2015-07-20 08:20:49
【问题描述】:

我正在编写一个 python 脚本,其中将运行一个循环并在多个目录中查找名称中带有字符串“_CriteriaOutput.html”的特定 html 页面。每个目录包含多个html文件和4-5个带有上述字符串的html文件。我想要做的是读取这些带有'_CriteriaOutput.html'名称的html文件并将其合并到一个不同的html文件中。我将在下面给出我的代码(到目前为止我做了什么)。此代码读取对我没用的 html 文件的源代码。我只想要文本(如果 html 文件中有的话)

import os
import fileinput

NightlyLogs = r'C:/Users/<user>/Desktop/Nightly_Logs/2015_07_16-0940'
dir = [fol for fol in os.listdir(NightlyLogs) if os.path.isdir(os.path.join(NightlyLogs, fol))]
dir = sorted(dir)
for folder in dir:
    HtmlLoc = r'%s/%s' %(NightlyLogs, folder)
    abc = [file for file in os.listdir(HtmlLoc) if file.endswith('_CriteriaOutput.html')]
    for one in abc:
        HtmlFile = r'%s/%s' %(HtmlLoc, one)
        open_file = open(HtmlFile, 'r')
        print open_file.read()

NightlyLogs 是一个包含具有 CL(更改列表)名称(例如 876564 或 865664 等)的文件夹的位置。每个 HTML 文件,例如 A_CriteriaOutput.html 或 B_CriteriaOutput.html 名称包含特定系列的信息(比如说 A 或 B 或 C 等),每个具有特定 CL 名称的文件夹包含类似的 _CriteriaOutput.html 文件,其中仅包含该 CL 的信息.我想制作一个表格,其中 CL 作为列,A、B、C、D、E 作为行,其中将包含该特定系列的信息。我试图具体说明,但如果您认为缺少某些信息,请帮助我学习。我会尽量提供尽可能多的信息。谢谢。

【问题讨论】:

  • 我在上述线程中找不到我的问题的完整答案,除了我的问题不同并且更多的是关于创建 html 表。
  • 那里的信息应该可以帮助您走得更远。它没有解释如何将信息整合到一个表格中,但它提供了关于如何从文件中读取信息的很好的信息。
  • 感谢您指出这一点。你其实是对的。一旦我得到任何答复,我将提供更多信息。

标签: python html


【解决方案1】:

所以你的问题是

我想制作一个表格,其中 CL 作为列,A、B、C、D、E 作为行,其中包含该特定系列的信息。

这样的?

    876564 | 865664 | ...
A |  ...   |  ...   | ...
B |  ...   |  ...   | ...

如果我正确阅读了您的问题,更改列表名称 (876564, ...) 是文件夹名称,而 A、B、... 是文件名的一部分,在 _CriteriaOutput.html 之前。

我会首先以与您类似的方式从所有文件中收集数据,最后您可以以任何您想要的方式打印它们。

import os
import fileinput

def pretty_print(change_list):
    change_names = []
    for category_name, category_list in sorted(change_list.items()):
        for change_name in category_list.keys():
            if change_name not in change_names: change_names.append(change_name)
    header = ['']
    header.extend(change_names)
    list_of_lists = []
    list_of_lists.append(header)
    for category, category_list in sorted(change_list.items()):
        titles = [category]
        for name in change_names:
            try:
                titles.append(category_list[name])
            except KeyError:
                titles.append('-')
        list_of_lists.append(titles)

    for line in list_of_lists:
        print '\t'.join(line)

change_list = {}
NightlyLogs = r'C:/Users/<user>/Desktop/Nightly_Logs/2015_07_16-0940'
dir = [fol for fol in os.listdir(NightlyLogs) if os.path.isdir(os.path.join(NightlyLogs, fol))]
dir = sorted(dir)
for folder in dir:
    HtmlLoc = r'%s/%s' %(NightlyLogs, folder)
    abc = [file for file in os.listdir(HtmlLoc) if file.endswith('_CriteriaOutput.html')]
    for one in abc:
        change_name = one.split('_')[0]
        if change_name not in change_list:
            change_list[change_name] = {}
        HtmlFile = r'%s/%s' %(HtmlLoc, one)
        open_file = open(HtmlFile, 'r')
        file_content = open_file.read()
        print change_name, '|', folder, '|', file_content
        change_list[change_name][folder] = file_content

print '\nTable of changes:'
pretty_print(change_list)

一些示例数据的输出(首先在读取时打印文件/文件夹名称/内容,然后使用pretty_print() 打印表格):

A | 876564 | foo
B | 876564 | foo B
A | 876565 | foobar
B | 876565 | foo
A | 876566 | bar
C | 876566 | bar C

Table of changes:
    876564  876565  876566
A   foo     foobar  bar
B   foo B   foo     -
C   -       -       bar C

【讨论】:

  • 感谢@adrianus 的回复。你理解正确,但看起来它仍然需要一些解决方法。我会尝试一下,让你知道输出。我也会尝试做出一些改变并想出一些东西。再次感谢。
  • @AnuragTiwary 不客气,如果仍有问题,请在此处发布。如果对您有帮助,请考虑选择一个接受的答案:-)
猜你喜欢
  • 2020-09-05
  • 2013-02-07
  • 2014-12-02
  • 1970-01-01
  • 2013-04-02
  • 1970-01-01
  • 2019-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多