How to read only an excerpt or part of text file?

I have ptx point cloud files, where I only need to read a line close to the start of the file.
As the files are very large, reading the whole file for this purpose is very slow.

Is there a way to read only an excerpt or a certain line from a text file in Grasshopper?

Not without custom programming. You’d have to open the file as a stream, and read it from disk until you’re done. All Grasshopper tools that read files always read the whole file.

Can you post an excerpt of such a ptx file and some info about what data you want to read from it?

Not at my computer but I’ve done it in python (if I remember correctly) with on line: open(filepath).readline(int)
Don’t know what happens in the background, but was always quit fast for me.
If I’m right it should be faster than using rhinoscriptsyntax and c# should be even faster.

Hello,

Yes you can do this with a python component, for instance to get only the 4th line without reading the rest of the file:


fname = r'C:\Users\KNAPP\AppData\Roaming\McNeel\Rhinoceros\5.0\scripts\boutons\colors.txt'
with open(fname) as f:
    f.readlines(3)  # skip 3 lines
    a = f.readline()
    print a
    print a.split() # split the line into a list of values

The file is automatically and safely closed after the with block, even if an error occurs during reading

Thanks, this would seem to be a good solution.

Unfortunately I need to make a total newbie follow-up question, as I’m totally unfamiliar with python (…planning to get to it some day):
I can’t really understand why my text at line 10 only gets read with the ‘readlines’ set to 196 - that doesn’t seem to match any character count or the like… this is something obvious?:

No this is strange - the readlines method should skip a given number of lines. I wonder whether this has something to do with the newline character used in your file? Could it be skipping 196 bytes rather than lines? I can see no reason why it should do this… It behaves normally for me on Rhino 5 for Windows in both the rhino python editor and a Grasshopper python component.

What happens if you hard code y = 9 in your script?

The 3rd line (0.000… etc) gets read (and the same happens if I set the slider to 9).
BTW, I removed the new line characters, and no change.

Strange - it seems close to, but different from, the number of characters.

And when I set y to 235, the script returns . That’s 39 in difference and the line does have 39 characters including the line end. So somehow this is tied to character count - the reason I said it isn’t, is because the sample here has 273 characters :thinking:

10060
4447
0.000000 0.000000 0.000000
1.000000 0.000000 0.000000
0.000000 1.000000 0.000000
0.000000 0.000000 1.000000
-0.997112 -0.075949 -0.000350 0.000000
0.075949 -0.997112 -0.000225 0.000000
-0.000331 -0.000251 1.000000 0.000000
69.879840 166.415007 2.318093 1.000000

…if you count just the actual characters, leaving out spaces, line returns and even dashes, the count is 236, though…

how about this as a workaround:

with open(x) as f:
    for _ in range(y):
        f.readline()  # skip y lines
    a = f.readline()
    print a

That does work on the 10 line sample text - but reading the 10th line in the actual 1,5Gb point cloud file is again very slow.

hmm and if you try to limit the size of the read buffer…?

open(fname, buffering = 1024)

Is the file saved on your local machine? If not then it may download a substantial portion (or all) of the file from the network before opening…

Okay, this seems to work better! I think I’ll go with this for now, and start on my python later to solve the first case… Thank you so much for your help!

1 Like