AppleStore data set: different column length "from csv import reader" vs. "with open() as"

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:
    if rows_and_columns:
        print('number of rows:', len(dataset))
        print('number of columns', len(dataset[0]))

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[0]
    ios = ios[1:]

I ran that code followed by
explore_data(apple,0,3, True)
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?


Hello @butzelaar, welcome to the community!

The .readlines() method returns all lines in the file, as a list where each line is an item in the list object. It essentially returns the whole file as just one list with each line of the file as each item of the list.

Therefore, this line of code in the explore_data function will return the length of the first item of the list which is also the first line of the file.

Whereas, the line of code below returns each line of the file as a list. Therefore, ios is a list of list containing each row/line of the file as a list.

Happy learning!

Thank you very much for that clear explanation. Makes total sense! Really appreciate it!

1 Like