From the course “Python for Data Science: Fundamentals” - Mission “Guided Project: Profitable App Profiles for the App Store and Google Play Markets”
The following function is provided to explore the dataset
def explore_data(dataset, start,end, rows_and_columns = False): dataset_slice = dataset[start:end] for row in dataset_slice: print(row) print('\n') if rows_and_columns: print('number of rows:', len(dataset)) print('number of columns', len(dataset))
I used the following “while open() as” code to open the dataset and run the explore_data function:
with open("AppleStore.csv" , "r", encoding="utf8") as appli: appli = appli.readlines() explore_data(appli,0,3, True)
Following the print-out of the header row, I expected a reasonable number of columns. Instead, it printed “179”
When I checked the solution notebook, I realised the files were opened by importing the csv reader:
from csv import reader opened_file = open('AppleStore.csv') read_file = reader(opened_file) ios = list(read_file) ios_header = ios ios = ios[1:]
I ran that code followed by
in my Jupyter notebook and then I also received the correct column length of 16.
What I don’t understand is why the “with open() as” code resulted in such a big discrepancy regarding column length?