Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "def csv_to_dataframe(file_path):\n",
- "\n",
- " import pandas as pd\n",
- " import os\n",
- " from io import StringIO\n",
- " \n",
- " # open the file \n",
- " \n",
- " file_reader = open(file_path)\n",
- "\n",
- " # create the empty dataframe\n",
- " \n",
- " headers = file_reader.readline()\n",
- " headers_ls = headers.split(',')\n",
- " df_global = pd.DataFrame(columns=headers_ls)\n",
- "\n",
- " # prepare the error file\n",
- " \n",
- " dir_path = os.path.dirname(file_path)\n",
- " file_name = os.path.basename(file_path)\n",
- " file_base, file_ext = os.path.splitext(file_name)\n",
- " \n",
- " error_file_noext = os.path.join(dir_path, file_base)\n",
- " error_file_path = f'{error_file_noext}_error.txt'\n",
- " #print(error_file_path) \n",
- " \n",
- " line_no = 2\n",
- " error_file = open(error_file_path, 'w+')\n",
- " error_lines = []\n",
- " \n",
- " # read one line at a time\n",
- " \n",
- " for line in file_reader:\n",
- " \n",
- " # if there are no errors, append it to dataframe\n",
- "\n",
- " try:\n",
- " #print(line)\n",
- " df_line = pd.read_csv(StringIO(line), header=None) # header none makes it treat 1 line like data\n",
- "\n",
- " # will error here if number of columns in df_line doesn't match number in df_global\n",
- " df_line.columns = df_global.columns\n",
- " #df_line.info() \n",
- " df_global = df_global.append(df_line)\n",
- "\n",
- " # if there is an error, store it and output it\n",
- " \n",
- " except Exception as ex:\n",
- " #print(f'{line_no} : {ex}')\n",
- " \n",
- " error_lines.append(line)\n",
- " error_file.write(line)\n",
- "\n",
- " line_no += 1\n",
- " #if line_no > 0:\n",
- " # break\n",
- " \n",
- " return df_global, error_lines"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {
- "scrolled": true
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<class 'pandas.core.frame.DataFrame'>\n",
- "Int64Index: 1525 entries, 0 to 0\n",
- "Data columns (total 10 columns):\n",
- " 1525 non-null object\n",
- "business_id 1525 non-null object\n",
- "cool 1525 non-null object\n",
- "date 1525 non-null object\n",
- "funny 1525 non-null object\n",
- "review_id 1525 non-null object\n",
- "stars 1525 non-null object\n",
- "text 1525 non-null object\n",
- "useful 1525 non-null object\n",
- "user_id\n",
- " 1525 non-null object\n",
- "dtypes: object(10)\n",
- "memory usage: 131.1+ KB\n"
- ]
- }
- ],
- "source": [
- "# call the function and inspect the DataFrame\n",
- "\n",
- "df_final, errors = csv_to_dataframe('Data/Yelp_Reviews_corrupt.csv')\n",
- "df_final.info()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "number of errors found: 4583\n"
- ]
- }
- ],
- "source": [
- "# check if there were errors in the file\n",
- "\n",
- "msg = f'number of errors found: {len(errors)}'\n",
- "print(msg)"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.7.3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement