Skip to content

Instantly share code, notes, and snippets.

@lakshay-arora
Last active April 9, 2020 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lakshay-arora/2ef1b49261b428dca3b8b86e0f5b5c0b to your computer and use it in GitHub Desktop.
Save lakshay-arora/2ef1b49261b428dca3b8b86e0f5b5c0b to your computer and use it in GitHub Desktop.
from pandas.io.parsers import ParserError
# create a list to store the dataframes
dataframe_list = []
# iterate through the folders 1 to 34
for folder in range(1, 35):
# create two boolean variables for both kind of exceptions.
parse_error = False
file_not_found = False
### notice that for folder i, we have file name "region_i"
### create the file name
try :
file_name = 'region_' + str(folder) + '.csv'
data = pd.read_csv('dataset/'+ str(folder) +'/' +file_name)
# if the error is ParserError, print file has incorrect format and set parse_error = True
except ParserError:
parse_error = True
data = pd.read_csv('dataset/'+ str(folder) +'/' +file_name, skiprows=4)
dataframe_list.append(data)
print(file_name, 'has incorrect format.')
# if the error is FileNotFoundError, print file is missing and set file_not_found = True
except FileNotFoundError:
file_not_found = True
print(file_name, 'is missing')
# if no exception occurs, append the dataframe to the list.
else:
dataframe_list.append(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment