1

I am unfamiliar with the csv library and the "with open" syntax that I see around online sources including stackoverflow use it for processing csv files.

Here is the "with syntax" I was talking about, it only seems to process the correct amount of lines with this code.

How would I do it with the first block of code shown?

6
  • 3
    What's the problem with using with, other than that you are unfamiliar with it? If unfamiliarity is the only issue, you can try becoming familiar with it by reading more about context mangers and code examples. Unless the closure of the file handle needs to be done outside the code block that opens it, there is no good reason not to use with. Commented Apr 11, 2019 at 21:00
  • 1
    Using the 1st code snippet is just bad practice. If you're gonna use open() without with you'll have to close() it as well! Commented Apr 11, 2019 at 21:04
  • 1
    There is a problem in the first function : in the second loop for value in values:, you don't use the value, and append the row to your data array. I think you should get rid of that loop and replace it with data.append(values). Commented Apr 11, 2019 at 21:13
  • This is an assignment. So that is the other reason why I cannot use that. He did not cover it in class so it’s more so when I use something he didn’t cover it would be an automatic zero. Commented Apr 11, 2019 at 21:31
  • And yes thank you I totally forgot to close it! Oops. I just started learning “Files” Commented Apr 11, 2019 at 21:31

1 Answer 1

2

Your second code snippet using the csv module gives you list of lists. To get the same functionality, you should read each line of the csv file, strip the line endings, split the line with the separation character, and append to your list.

def file(file_name):
    f = open(file_name, "r")
    f.readline()
    data = []
    for row in f:
        values = row.strip().split(",")
        data.append(values)
    f.close()
    return data
Sign up to request clarification or add additional context in comments.

5 Comments

I am still having trouble because this code is based on this file: docs.google.com/spreadsheets/d/… But, for example, if you call to row 4 like colleges[4]. It gives you the data in row 6
colleges = file("colleges.csv"). somecollege = colleges[4]. print(somecollege). It gives the wrong row
@Angel Baby, The file name was hard coded in the function earlier. I just used your function in there. Changed it now. I will be testing with the file you posted.
It looks like it is working fine. Here is what the code is doing. It is ignoring the headers and reading from the second like. Now, you asked for colleges[4]. Remember that the index starts from '0'. So, what you are looking at is the row '5'. If you consider the headers, you are looking at row '6' in that google chart. Does it make sense?
Yes it does. Thank you so much

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.