【问题标题】:loading a multiple .txt files in to python as dataframe将多个 .txt 文件作为数据框加载到 python
【发布时间】:2020-11-07 02:46:21
【问题描述】:

我正在努力将多个 .txt 文件加载到我桌面上的 python 中。我对 Python 完全陌生。我的目标是加载多个 .txt 文件,这些文件保存在同一目录中。 .txt 文件是纯文本。在此先感谢您的帮助!

【问题讨论】:

  • 问题更正:我想将数据作为数据框导入
  • 能否详细说明dataframe的使用?
  • 嗨,弗洛里安,我想为每个文件使用 gensim 进行主题建模。这就是为什么我需要作为数据框导入。
  • 我正在努力将多个 .txt 文件加载到我桌面上的 python 中。我对 Python 完全陌生。我的目标是加载多个 .txt 文件,这些文件保存在同一目录中。 .txt 文件是纯文本。我想将数据作为数据框导入。我想为每个文件使用 gensim 进行主题建模。这就是为什么我需要导入为数据框。提前感谢您的帮助!

标签: python file dataframe loading


【解决方案1】:

你可以这样做。


from collections import defaultdict
from pathlib import Path
import pandas as df

my_dir_path = "/parh/to/folder"

results = defaultdict(list)
for file in Path(my_dir_path).iterdir():
    with open(file, "r") as file_open:
        results["file_name"].append(file.name)
        results["text"].append(file_open.read())
df = pd.DataFrame(results)

【讨论】:

    【解决方案2】:

    这可能是不必要的长,但如果您需要,会为文件名创建另一列:

    import os
    import csv
    import pandas as pd
    main_folder = 'path\\to\\some_folder'
    
    def get_filename(path):
        filenames = []
        files = [i.path for i in os.scandir(path) if i.is_file()]
    
        for filename in files:
            filename = os.path.basename(filename)
            filenames.append(filename)
        return filenames
    
    files = get_filename(main_folder)
    
    with open('some.csv', 'w',  encoding = 'utf8', newline = '') as csv_file:
        for _file in files:
    
            file_name = _file
            with open(main_folder +'\\'+ _file,'r') as f:
                text = f.read()
    
                writer = csv.writer(csv_file)
                writer.writerow([file_name, text])
    
    df = pd.read_csv('some.csv')
    
    
     # ...then whatever...
    

    【讨论】:

    • 您好穆莱希,非常感谢您的回复。 'some_folder' 是指我的文件所在的路径吗?谢谢
    • 是的,就是路径。我现在将编辑。但是,如果@Florian Bernard 的回答对您有用,那看起来会更好更短。
    • 感谢您的宝贵时间。是的,弗洛里安的回答对我来说很好。我也会试试你的。
    【解决方案3】:

    我会这样做的。

    import glob
    
    read_files = glob.glob('C:\\your_path_here\\*.txt')
    
    with open('result.txt', 'wb') as outfile:
        for f in read_files:
            with open(f, 'rb') as infile:
                outfile.write(infile.read())
    

    我有 5 个如下所示的文本文件:

    FName,LName,Address
    Jim,Bentz,34 Holloway La.
    George,Hororitz,76 Ridge Dr.
    Eric,Schimtz,11 Main St.
    

    最终的结果是这样的:

    FName,LName,Address
    Jim,Bentz,34 Holloway La.
    George,Hororitz,76 Ridge Dr.
    Eric,Schimtz,11 Main St.
    FName,LName,Address
    Jim,Bentz,34 Holloway La.
    George,Hororitz,76 Ridge Dr.
    Eric,Schimtz,11 Main St.
    FName,LName,Address
    Jim,Bentz,34 Holloway La.
    George,Hororitz,76 Ridge Dr.
    Eric,Schimtz,11 Main St.
    FName,LName,Address
    Jim,Bentz,34 Holloway La.
    George,Hororitz,76 Ridge Dr.
    Eric,Schimtz,11 Main St.
    FName,LName,Address
    Jim,Bentz,34 Holloway La.
    George,Hororitz,76 Ridge Dr.
    Eric,Schimtz,11 Main St.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多