简体   繁体   English

将 multiple.txt 文件加载到 python 作为 dataframe

[英]loading a multiple .txt files in to python as dataframe

I was struggling to load multiple.txt files in to python that are in my desktop.我正在努力将多个.txt 文件加载到我桌面上的 python 中。 I am totally new to Python.我对 Python 完全陌生。 My goal is to load multiple.txt files, which is saved in the same directory.我的目标是加载多个.txt 文件,这些文件保存在同一目录中。 The.txt files are plain texts. .txt 文件是纯文本。 Thanks in advance for your help!在此先感谢您的帮助!

You could do something like this.你可以做这样的事情。


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)

This might be unnecessarily long but creates another column for the filenames, if you need:如果需要,这可能会不必要地长,但会为文件名创建另一列:

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...

I would do it like this.我会这样做。

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())

I have 5 text files that look like this:我有 5 个如下所示的文本文件:

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

The final result looks like this:最终结果如下所示:

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM