【问题标题】:Read CSV, if text match, open a html file with matching file name, and copy in text读取CSV,如果文本匹配,打开一个匹配文件名的html文件,并复制到文本中
【发布时间】:2019-11-13 19:25:04
【问题描述】:

好吧,我想我只是缺少连接器,我对 python 很陌生。

目标:读取 CSV
读取目录中的所有文件名
如果 index(x) 处的 ROW = 目录中的文件名,则
打开 HTML 文件,并将 index(x) 处的文本替换为 HTML 文件中的文本

到目前为止的代码:

import fileinput
import csv
import os
import sys
import glob
from bs4 import BeautifulSoup

htmlfiles_path = "c:\\somedirectory\\" #path to directory containing the html files
filename_search = glob.glob("c:\\somedirectory\\*.HTM") #get list of filenames

#open csv

with open ('content.csv', mode='rt') as content_file:
    reader = csv.reader (content_file, delimiter=',')
    for row in reader:
        for field in row:
            if filename_search(some matching logic i am stuck on):
                for htmlcontentfile in glob.glob(os.path.join(path, ".HTM")):
                    markup(htmlcontentfile)
                    soup = BeatifulSoup(open(markup, "r").read())
                        content_file.write(soup.get_text())
                #i think something else goes here

我让 csv 阅读器工作,并让 glob 提取文件名列表,但在连接这些文件时遇到了一些问题。任何帮助都会很棒。

我查找了其他问题,其中一些代码基于此,但我没有在 python 中找到任何关于这个挑战的东西。如果有,请指出我正确的方向!

EDIT1:我在我的代码中打开的 csv 中使用“wt”。但这不是卡住的地方。

我有一个装满文件的文件夹。示例:

内容/d100.htm
内容/d101q.htm
内容/d102s.htm

还有 CSV:
CSV 示例
CSV 文件:

标题名称位置
加州总统 d100.html

目标:打开 csv,在 Location 下查找文件夹“content”中任何文件的匹配项
如果找到匹配项,打开相应的 HTM 文件,只解析文本
将csv中的字段替换为文件的文本内容

这有意义吗?

【问题讨论】:

  • 您确定content_file.write(soup.get_text()) 中的标识正确吗?
  • 请在此处解释您要达到的目标。
  • 该文件已以rt模式打开,这意味着它是以只读方式打开的,请将其更改为'rt'并重试。
  • 如果您查看 csv 的 Python 文档,您将了解如何正确打开文件。
  • @Salman,我觉得我已经准确地解释了我想要做什么。打开 CSV 查找“filename.html”替换文本,使用 HTML 文件的内容,并匹配名称

标签: python beautifulsoup glob


【解决方案1】:

答案:

1) @barny 如果我没有运行代码,我不会在这里发帖。我为误解了我所寻找的内容而道歉。

无论如何,我通过稍微更改问题陈述并使用 Excel 完成它来解决这个问题。

原问:

CSV 与

文字 |答案 |目标文件内容

一些文字 |参考文件 001.htm |
其他一些文字|请参阅文件 002.htm |

找到文件,并将内容解析到它旁边的列。

稍微改了问:

将所有 htm 文件解析为 csv,并列出它们各自的文件名。然后使用 Excel 来匹配内容。

Excel 已经有一个函数 index(match()) 可以完成我请求的第二部分,而不是让 BSoup 或 Python 完成匹配工作。所以我让 Python 和 Bsoup 打开每个 HTML 文件,并将其放入 CSV。我也在另一列中携带了很长的文件名。像这样:

文件:
内容/001.htm

内容/002.htm

内容/003.htm

CSV 输出的预期格式:

HTML 文件的内容 |文件名

代码:

import fileinput
import csv
import os
import sys
import glob
from bs4 import BeautifulSoup

path = "<the path>"


def main():
   for filepath in glob.glob(os.path.join('<the path>', '*.HTM')): #find folder containing html files 
    with open(filepath) as f:
        contentstuff = f.read() #find an html file, and read it
        soup = BeautifulSoup(contentstuff, "html.parser") #parse the html out
        with open (path + '\\htmlpages.csv', 'a', encoding='utf-8', newline='') as content_file:
            writer = csv.writer (content_file, delimiter=',') #start writer for file content to CSV
            fp = filepath[-12:] #trim the file name to necessary name
            for body_tag in soup.find_all('body'):
                bodye = (body_tag.text.replace("\t", "").replace("\n", "")) #deal with necessary formatting between Bsoup and Excel
                print(bodye) #show me the work
                writer.writerow([bodye, fp])  #do the actual writing

在内容进入 CSV 之后,我使用 index(match()) 将核心文件中的文件名与新的 CSV 配对。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多