【问题标题】:python read all files from a folder and write the file name and other info into a txt filepython从文件夹中读取所有文件并将文件名和其他信息写入txt文件
【发布时间】:2017-10-29 09:50:29
【问题描述】:

我有 30911 个 html 文件。我需要进行网页抓取,然后将信息保存到名为 index.txt 的 txt 文件中。 它应该看起来像

filename1, title, t1, date, p1
filename2, title, t1, date, p1
filename3, title, t1, date, p2
and so on...

我只想要文件名,但输出给了我路径+文件名。

【问题讨论】:

    标签: python file io store file-format


    【解决方案1】:

    您的问题是文件名实际上是文件路径,为了获取文件名,您可以使用 os 模块

    os.path.basename('filepath')
    

    所以为了写入文件:

    indexFile.write(os.path.basename(filename)+ ', ' + title.get_text(strip=True) + ', '+ ticker.get_text(strip=True) + ', ' + d_date.get_text(strip=True) + ', ' + parti_names + '\n')
    

    【讨论】:

    • 我添加了使用方法
    • glob.glob 中的文件名(os.path.join(path, '*.html')): print os.path.basename(filename)
    • 如果你完成了,别忘了点赞并标记为答案,要知道
    【解决方案2】:

    你可以使用:

    path = 'C:/Users/.../.../output/'
    #read html files
    for filename in glob.glob(os.path.join(path, '*.html')):
        soup = bs4.BeautifulSoup(open(filename).read(), "lxml")
        title = soup.find('h1')
        ticker = soup.find('p')
        d_date = soup.find_all('div', {"id": "a-body"})[0].find_all("p")[2]
    
        try:
            def find_participant(tag):
                return tag.name == 'p' and tag.find("strong", text=re.compile(r"Executives|Corporate Participants"))
    
            participants = soup.find(find_participant)
            parti_names = ""
            for parti in participants.find_next_siblings("p"):
                if parti.find("strong", text=re.compile(r"(Operator)")):
                    break
                parti_names += parti.get_text(strip=True) + ","
        except:
            indexFile = open('C:/Users/.../output1/' + 'index.txt', 'a+')
            indexFile.write(filename + ', ' + title.get_text(strip=True) + ', '+ ticker.get_text(strip=True) + ', ' + d_date.get_text(strip=True) + ', ' + 'No participants' + '\n')
        else:
            participants = soup.find(find_participant)
            parti_names = ""
            for parti in participants.find_next_siblings("p"):
                if parti.find("strong", text=re.compile(r"(Operator)")):
                    break
                parti_names += parti.get_text(strip=True) + ","
            indexFile = open('C:/Users/.../output1/' + 'index.txt', 'a+')
            indexFile.write(os.path.basename(filename) + ', ' + title.get_text(strip=True) + ', '+ ticker.get_text(strip=True) + ', ' + d_date.get_text(strip=True) + ', ' + parti_names + '\n')
            indexFile.close()
    

    【讨论】:

      【解决方案3】:

      ntpath 是另一个用于从路径获取基本名称的模块。

      >>> import ntpath
      >>> ntpath.basename('C:/Users/.../output1/' + 'index.txt')
      'index.txt'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-07
        • 2018-06-06
        • 1970-01-01
        • 2016-10-14
        • 1970-01-01
        • 2018-08-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多