Im quite new to python and have some issues with what I presume really is a quite easy task…?
Im trying to import a file and filter out (get rid of) all the empty lists so that the remaining data will be more useful, but the "filter(None, list1) doesn’t seem to do anything at all? What am I missing here?
Really greatful for any tips on how to do this.
str = open(filePath) for i in str: list1 = i.split() list2 = filter(None, list1) #does this do anything at all!?? print list2
…and here is what it looks like:
I guess you could do something like this:
str = open(filePath)
for i in str:
list1 = i.split()
Otherwise, if you could also filter out blank lines upstream of split() I think:
file = open(filepath)
for line in file:
if line != "\n":
Awesome, thank you!!! finally I get a grip on this. Your first example works really well, the second one seems to just add them on top of each other so that if we have items items , , , it returns:
I haven’t thoroughly tested this, but with automatically closes your file, which is good practice.
with open(Path, 'r') as file:
lists = [line.split() for line in file if not line.isspace()]
what is the function of ‘r’ in open()? That code in contrast to the previously suggested returns the file as one grasshopper index, but still splits it into multiple python items. Do you know what the relation between Grasshopper paths/indexes and python lists and strings are? They seem to be slightly different things?
‘r’ means open for reading. This ensures you won’t accidently write to the file.
actually, looking at it again I realize that its “list1” that I want if len(list1)>0:
The appended list just mass additions everything and messes up the grasshopper indexes. Do anyone know what the relation is between python list & strings and GH paths and indices are and how to control them?
What I want is to the left in the picture, but it only works when I print list1 within the if statement. appending it wont help me bring it out of that as you see in the picture to the right… =/
here is what it looks like:
Not sure, but I think you should search for something like ‘python grasshopper build data tree’.