Чтение нескольких файлов txt в Dict в Pandas фрейм данных - PullRequest
0 голосов
/ 16 июня 2020

Я пытаюсь загрузить несколько файлов txt в фрейм данных. Я знаю, как загружать URL-адреса, csv и excel, но я не смог найти никакой справки о том, как загрузить несколько файлов txt в фрейм данных и сопоставить их со словарем или наоборот.

текстовый файл не разделен запятыми или табуляцией просто простой текст, содержащий текст песни.

Я проверил pandas документы, приветствую любую помощь.

https://pandas.pydata.org/pandas-docs/stable/reference/io.html

Идеально фрейм данных

фрейм данных, который я надеюсь достичь, будет похож на этот пример

                 |                                                        lyrics
    -------------+-----------------------------------------------------------------------------------------
    bonjovi      |    some text from the text files HiHello! WelcomeThank you Thank you for coming.
    -------------+---------------------------------------------------------------------------------------
    lukebryan    |    some other text from the text files.Hi.Hello WelcomeThank you Thank you for coming. 
    -------------+-----------------------------------------------------------------------------------------
    johnprine    |    yet some text from the text files. Hi.Hello WelcomeThank you Thank you for coming. 

Basi c пример структура папок / текст /

urls = 

    'lyrics/bonjovi.txt',
    'lyrics/lukebryan.txt',
    'lyrics/johnprine.txt',
    'lyrics/brunomars.txt',
    'lyrics/methodman.txt',
    'lyrics/bobmarley.txt',
    'lyrics/nickcannon.txt',
    'lyrics/weeknd.txt',
    'lyrics/dojacat.txt',
    'lyrics/ladygaga.txt',
    'lyrics/dualipa.txt',
    'lyrics/justinbieber.txt',]

мусульманские имена

bands = ['bonjovi', 'lukebryan', 'johnprine', 'brunomars', 'methodman', 'bobmarley', 'nickcannon', 'weeknd', 'dojacat', 'ladygaga', 'dualipa', 'justinbieber']

Откройте текстовые файлы файлы находятся в каталоге lyrics /, откуда я запустил свой блокнот Jupyter.

for i, c in enumerate(bands):
     with open("lyrics/" + c + ".txt", "wb") as file:
         pickle.dump(lyrics[i], file)

Дважды проверьте, правильно ли загружены данные

data.keys()

надеюсь получить такой результат

dict_keys (['bonjovi', lukebryan, johnprine, brunomars, methodman, bobmarley, nickcannon, weeknd, doja cat ',' ladygaga ',' dualipa ',' justinbieber '])

# Combine it!
data_combined = {key: [combine_text(value)] for (key, value) in data.items()}


# We are going to change this to key: artist, value: string format
def combine_text(list_of_text):
    '''Takes a list of text and combines them into one large chunk of text.'''
    combined_text = ' '.join(list_of_text)
    return combined_text

Мы можем сохранить его в формате словаря или поместить в pandas фрейм данных

import pandas как pd

pd.set_option('max_colwidth',150)

data_df = pd.DataFrame.from_dict(data_combined).transpose()
data_df.columns = ['lyrics']
data_df = data_df.sort_index()
data_df

1 Ответ

1 голос
/ 17 июня 2020
import os
import re
import pandas as pd

#get full path of txt file
filePath = []
for file in os.listdir("./lyrics"):
    filePath.append(os.path.join("./lyrics", file))

#pull file name from text file with regex, capturing the text before the .txt   
fileName = re.compile('\\\\(.*)\.txt')

#make empty dict Data with the key as the file name, and the value as the words in the file.
data = {}
for file in filePath:
    #capturing file name
    key = fileName.search(file)
    with open(file, "r") as readFile:
        # note that key[1] is the capture group from our search, and that the text is put into a list.
        data[key[1]] = [readFile.read()]

#make dataframe from dict, and rename columns.
df = pd.DataFrame(data).T.reset_index().rename(columns = {'index':'bands', 0:'lyrics'})

Вот как бы я это сделал. Обратите внимание, что я обобщил манипуляции с файлами, поэтому мне не нужно беспокоиться о том, чтобы вручную составлять список для ключей и убедиться, что все совпадает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...