0

So I have a problem to solve for a practice task. The task is to develop a function which reads csv data from a file into memory, but we cannot use any libraries to do so. So i can't use csv reader, Pandas, NumPy etc.

This is what I have come up with, but it does not work as it says 'csv_list is not defined'. I am a bit stuck on where to go from here, and have maninly only coded using libraries, so coding manually and developing functions myself are a struggle! I have looked on here for any solutions but none of them seem to work / they use libraries which I cannot use. If anyone has a way to do this I would be so grateful!

#define read csv
def read_csv (file_name):
    with open(file_name) as f:
        csv_list = [[val.strip() for val in r.split (",")] for r in f.readlines()]
#convert file to dictionary structure 
(_, *header), *data = csv_list
csv_dict = {}
for row in data:
    key, *values = row
    csv_dict[key] = {key: value for key, value in zip(header, values)}
#insert name of file to be read by user 
read_csv (task1.csv)
3
  • Is your csv_list initialized our of the scope of the code snippet? Commented Jan 12, 2023 at 13:58
  • 2
    your indentation is incorrect. some code falls outside the function.You need to indent all code except the last line inside the function. Then your function should return the contents of the file, so you can use them. Commented Jan 12, 2023 at 13:58
  • You don't need to do for r in f.readlines(). Just do for r in f. f is an iterator that iterates over the file's lines anyway Commented Jan 12, 2023 at 14:44

1 Answer 1

1

The answer to why you see the error is that csv_list is local to the method read_csv(file_name). Rather than set that variable, perhaps you want to just return the result of the comprehension. Then you can get the value you seek when you call that method. Note, you need to call it much sooner than you currently do.

This will give you a dictionary, but I'm not sure it is structured in the way you likely want. I would expect a list of dictionaries after reading a CSV not what you are producing but that is your call.

#define read csv
def read_csv (file_name):
    with open(file_name) as f:
        return [[val.strip() for val in r.split (",")] for r in f.readlines()]

(_, *header), *data = read_csv("small.csv")

#convert file to dictionary structure 
csv_dict = {}
for row in data:
    key, *values = row
    csv_dict[key] = {key: value for key, value in zip(header, values)}

print(csv_dict)

If it were me, I might make the following tweaks to get a result that was list based. I'm also going to get rid of the stuff like *header while at it:

def read_csv(file_name):
    with open(file_name) as f:
        yield from [[c.strip() for c in r.split(",")] for r in f]

rows = read_csv("small.csv")
header_row = next(rows)
csv_dict = [dict(zip(header_row, row)) for row in rows]

print(csv_dict)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.