问题
I was struggling to load multiple .txt files in to python that are in my desktop. I am totally new to Python. My goal is to load multiple .txt files, which is saved in the same directory. The .txt files are plain texts. Thanks in advance for your help!
回答1:
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"] = file.name
results["text"].append(file_open.read())
df = pd.DataFrame(results)
回答2:
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...
来源:https://stackoverflow.com/questions/58467374/loading-a-multiple-txt-files-in-to-python-as-dataframe