2

I got a problem with this code:

import csv
with open('gios-pjp-data.csv', 'r') as data:
    l = []
    reader = csv.reader(data, delimiter=';') 
    next(reader) 
    next(reader)  # I need to skip 2 lines here and dont know how to do it in other way
    l.append(# here is my problem that I will describe below)

So this file contains about 350 lines with 4 columns and each one is built like this:

Date ; float number ; float number ; float number

Something like this:

2017-01-01;56.7;167.2;236.9

Now, I dont know how to build a function that would append first float number and third float number to the list on condition that its value is >200. Do you have any suggestions?

2
  • Can you provide your expected output? Commented Jan 20, 2018 at 11:27
  • output : l= [236.9] Commented Jan 20, 2018 at 14:57

1 Answer 1

2

List comprehentions if you don't have too many items in the file.

l = [x[1], x[3] for x in reader if x[1] > 200]

Or a similar function that would yield each line, if you have a huge number of entries.

def getitems():
    for x in reader:
        if x[1] > 200:
            yield x[1], x[3]

l = getitems()  # this is now an iterator, more memory efficient.
l = list(l)  # now its a list
Sign up to request clarification or add additional context in comments.

1 Comment

you can do a gencomp as well no need for yield: l = (x[1], x[3] for x in reader if x[1] > 200)

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.