Hello. I have a set of strings in Python and want to send them to .txt files
But to do that in Python is a pain in the neck
So I want to take the strings from Python a send them to Mathematica,
that would make everything very easy.
Is there a way to do this?
Send the string from Python to Mathematica.
Or retrieve the Python strings from Mathematica?
What does this mean? In your Python code you have some variables assigned to string values? Can’t you just copy paste from your Python code listing into a Mathematica notebook?
…and want to send them to .txt files, but to do that in Python is a pain in the neck
I just don’t sympathize with this statement. Python has all of the typical file open/write/read/close functions. Maybe there is something more complicated going on that you can explain for us.
Send the string from Python to Mathematica? Or retrieve the Python strings from Mathematica?
How are you envisioning this working? Do want some sort of foreign function interface you can call from Python so that you can use Mathematica’s Export function? That seems much more complicated than jusing Python’s write function. Or do you want Mathematica to parse your Python source code and pull out strings to export? That also seems much more complicated than just using Python’s write function.
I am not asking for sympathy but for some kind of help. And this is not a Python but a Wolfram community
In Mathematica, things are simply easier.
“Do want some sort of foreign function interface you can call from Python so that you can use Mathematica’s Export function?”
Exactly, I simply coordinate all the things I do from Mathematica
No, it is not so easy because the strings can have encoding issues.
And besides, I simply want to know how to do it from Mathematica.
The specific issues I am facing are easier to deal with from Mathematica.
If you want to display your knowledge, I am ok with this. But it does not help.
So: withdrawing the pain in the neck clause, can this operation be done from Mathematica?
And then how?
Okay, in Mathematica, if you want to save a string to a file, you could use Export:
stringToExport = "this is a test string";
Export[<filepath>, stringToExport]
When you said that Mathematica “would make everything very easy”, I assumed that meant that you already knew how to do it in Mathematica.
If you did already know how to use Export, then I don’t know what your question is now. Earlier you said that you were indeed asking for FFI, and Rohit gave you links to relevant resources, but now you seem to be saying that those weren’t helpful.
I’m also confused about why, if everything is easier in Mathematica, you aren’t just doing this string processing in Mathematica in the first place.
Anyway, I’m sorry, but I just have no idea what you’re trying to accomplish nor what the specific hurdles are that you’re encountering.
Connecting to Python is quick and easy in current versions of Wolfram Language. Here is a simple example that shuttles strings back and forth between the two languages. First start a Python external session:
session = StartExternalSession["Python"]
Define a function in Python that does something with a string (reversing the characters):