Reading csv file and storing the data in dictionary where the values will be in the list without importing library

why cant i see the output, is there something wrong?

def readtask1File(fileName=“books.csv”):
with open(fileName) as infile:
column_names = infile.readline()
keys = column_names.split(“,”)
number_of_columns = len(keys)
list_of_dictionaries =
data = infile.readlines()
list_of_rows =

    for row in data:
    for item in list_of_rows:
        row_as_a_dictionary = {}
        for i in range(number_of_columns):
            row_as_a_dictionary[keys[i]] = item[i]

for i in range(len(list_of_dictionaries)):

Please fix your code quoting to include all the code.

Is there a reason you are not using the python csv module to read the csv file?

I am sorry, but requirement (‘without importing library’) makes it feel like homework. There is battery included module and this should be used.

Some observations: there is no need to iterate over indices in Python. You can do it directly on items:

for item in iterable:
    # do something

If csv module not to be used then there are several other built-ins and concepts in Python what can be used. Namely zip, dict, and list comprehension.

In natural language: open the file, read first line as keys and create list of dictionaries from other lines where keys are combined with values on these lines.

# file named 'data.csv' with following content

with open('data.csv') as f:
    headers = next(f).rstrip().split(',')
    data = [dict(zip(headers, line.rstrip().split(','))) for line in f]

# data
[{'a': '1', 'b': '2', 'c': '3'}, 
{'a': '4', 'b': '5', 'c': '6'}]

can we keep the values that are associated to A together and same goes for B and C? rather then printing a: 1 b : 2 c: 3. it should look like this ‘a’: ‘1’,‘4’

One additional zip (which is ofter referred as rows to columns) could make something like that happen:

with open('data.csv') as f:
    headers = next(f).rstrip().split(',')
    stream = (line.rstrip().split(',') for line in f)
    data = dict(zip(headers,zip(*stream)))

# data
{'a': ('1', '4'), 
 'b': ('2', '5'), 
 'c': ('3', '6')}

sorry but one more question what if i want to use the code that you provided above but for the values inside the dictionary, we add them in a list as integers rather than keeping them as strings?
I appreciate your support thank you!

Some examples:

>>> nums = ['1', '2', '3']
>>> list(map(int, nums))
[1, 2, 3]
>>> [int(num) for num in nums]
[1, 2, 3]