Message Boards Message Boards

0
|
3138 Views
|
6 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Take a string from Python

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?

Any help is very appreciated

6 Replies
Posted 1 year ago

I have a set of strings in Python

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.

POSTED BY: Eric Rimbey

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

I agree with Eric, writing an array of strings to a text file is trivial in Python not a "pain in the neck".

with open('file.txt', 'w') as f:
  for s in strings:
    f.write(f"{s}\n")

If that is your only use case then it seems like unnecessary complexity to involve WL in the process.

If your needs are more complex then take a look at this and this.

POSTED BY: Rohit Namjoshi

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?

Hope my answer is not blocked this time.

Posted 1 year ago

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.

You mentioned encodings, maybe that's your real question. Maybe this will be helpful: http://reference.wolfram.com/language/ref/CharacterEncoding.html

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.

POSTED BY: Eric Rimbey

Thank you for this. But next time do not lecture me for asking.

It doesn't help anybody, certainly not Mathematica

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract