import pandas as pd from bs4 import BeautifulSoup with open("sorted_data/books/positive.review") as file: soup_pos = BeautifulSoup(file, "lxml") texts_pos=[] for review in soup_pos.find_all("review"): texts_pos.append(review.review_text.text) books_pos = pd.DataFrame({"text":texts_pos, "label":1}) books_pos.text = books_pos.text.replace("^\n", "", regex=True) from sklearn.model_selection import train_test_split books_pos_train, books_pos_test = train_test_split(books_pos, train_size=800, random_state=41) # Not OK to sart with, OK after open+save in Sublime # (encoding situation) with open("sorted_data/books/negative.review") as file: soup_neg = BeautifulSoup(file, "lxml") texts_neg=[] for review in soup_neg.find_all("review"): texts_neg.append(review.review_text.text) books_neg = pd.DataFrame({"text":texts_neg, "label":0}) books_neg.text = books_neg.text.replace("^\n", "", regex=True) books_neg_train, books_neg_test = train_test_split(books_neg, train_size=800) pd.concat([books_pos_train, books_neg_train]).to_csv("sorted_data/books/train.csv", index=False) pd.concat([books_pos_test, books_neg_test ]).to_csv("sorted_data/books/test.csv", index=False)