I'm developing a program that reads among other things a csv with csv.reader(). I want to save variables that are matching one of two conditions in two different classvariables. Because i like writing list comprehensions and i'm trying to write my code in a more pythonic way, i was wondering if this is the right approach. csv.reader() seems to be a generator object, because i can iterate over it just once. So for the second condition i'm creating the reader-object a second time. That seems like a ressource waste to me. Of course i could write it with a normal for loop and different if cases. But is there a more pythonic way to write this piece of code?
with open(file, "r") as f:
reader = csv.reader(f, delimiter=",")
self.xitireq = [row[0] for row in reader if "xiti" in row[0]]
with open(file, "r") as f:
reader = csv.reader(f, delimiter=",")
self.apireq = [row[0] for row in reader if "https://www.blabal.de/api" in row[0]]
If i write the code like that, self.apireq is empty:
with open(file, "r") as f:
reader = csv.reader(f, delimiter=",")
self.xitireq = [row[0] for row in reader if "xiti" in row[0]]
self.apireq = [row[0] for row in reader if "https://www.blabal.de/api" in row[0]]