Build a Simple Text File using Grasshopper Python

I would like to build a text file using data obtained in grasshopper.

The ultimate goal is for each line of the text file to look like this:
{ “x”: x component from point , “y”: *y component from point *, “angle”: angle value from atan2 function},

For example:

{ “x”: 25, “y”: 25, “angle”: 150},
{ “x”: 25, “y”: 26, “angle”: 151},

I can print this data in this format in the python script editor for each python script cycle. Is there a way I can add a new line to a block of text with each cycle and then stream it to a .txt? So far whenever I try GH wants follow its own index structure for handling points.

You can use the native Python file open, write, and close functions. Here’s a basic example of appending text onto an existing .txt file, but one can of course also code the creation of the file:


230810_WriteTextToFile_00.gh (3.4 KB)

2 Likes

Hi Eric,

You may need to be more specific. But the simplest solution for occasional use is to output your text to a panel and stream the panel content to a text file.

image

Is that adequate, or do you want more automation for highly repetitive use?

Regards
Jeremy

1 Like

Hey Eric. The others have covered the options well. I would just add that string-keyed dictionaries, such as yours, are well suited to JSON. JSON files are text files. Have I imagined that there’s a JSON component, or was that from a plug-in? Or all in Python, you could do:

import json
with open(text_file_name, 'wt') as f:
    json.dump(snakeData, f)

or equally well:

     json.dump([snakeData_1, snakeData_2], f)