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},
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:
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.
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)