0

I would like to compare rows 1-3 and rows 9-16 from my Excel spreadsheet. This is the code I have right now, but I only know how to do 1-3, not both 1-3 and 9-16:

with open('Weather.csv', 'r') as file1:
     val = list(csv.reader(file1))[i]
     val1 = val[0:3]

I tried this:

with open('Weather.csv', 'r') as file1:
     val = list(csv.reader(file1))[i]
     val1 = val[0:3][9:16]

but it doesn't work; nothing happens.

3
  • Do you get any out out or error messages? Commented Apr 25, 2015 at 22:46
  • No I do not get an error message Commented Apr 25, 2015 at 22:49
  • val1 = val[0:3] + val[9:16]? Commented Apr 25, 2015 at 22:50

1 Answer 1

1

Well without knowing what data your file contains, I'm going to assume each row contains only 2 comma separated values (weather, temperature).

So first:

val1 = val[0:3][9:16] will not do anything because what you're saying is to slice the val list between index 0 and 3 and then slice that slice with starting index of 9 to 16, which won't exist (because you just sliced it with 0:3), therefore you'll get an empty string in return.

If you'd like to compare multiple rows by batching those rows together, you'd probably want to do something along the lines of:

compare1 = val[0:3]
compare2 = val[9:16]

And then perform whatever 'comparison' level operations you'd like to do from there. I'm not sure why you'd do that though instead of just slicing the original val value however you want for each desired comparison.

For example, if you know: row 1 contains 'New York, 60' and row 12 contains 'San Francisco, 75'

And you'd like to compare the temperatures of these cities you can run:

val[0][1] < val[11][1] which would return 'True'

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.