Import python list from .txt file into Mathematica

In summary, the conversation is about importing a list of numbers from a text file in Mathematica. The file, k.txt, contains numbers enclosed in square brackets, making it difficult to extract the numbers. The individual is using ReadList["k.txt", String] but is unsure how to extract the numbers from the string. They suggest exporting the list from Python without the square brackets and using Import["k.txt", "CSV"] in Mathematica. Another individual suggests using output.write(str(result)[1:-1]) in Python to remove the square brackets before exporting. The person is grateful for the help received from PF and plans to make a donation to support the website.
  • #1
member 428835
Hi PF!

I am trying to import k.txt file into Mathematica as a list of numbers. The k.txt looks like this:

[5.315967917338153e-06, -3.204321972222162e-08, 3.6041782425371564e-09, -3.853442465663655e-08, 8.699510604529962e-07, -1.4284341965847237e-08, 2.855341882658808e-09, -1.1022087781880504e-08, 2.884254278951357e-07]

Currently I'm trying ReadList["k.txt", String] but this imports the entire list as a string (obviously) which I then don't know how to extract numbers. Any ideas? Alternatively, since k.txt is being created in Python, perhaps there's a way to export python list to mathematica list (I've googled it but can't seem to get it working).
 
Technology news on Phys.org
  • #2
How are you exporting the list in Python?
 
  • #3
DrClaude said:
How are you exporting the list in Python?
Sorry, I'm using
Python:
    with open("k.txt", "w") as output:
        output.write(str(result))

where result is a list of numbers. Is there a better way so Mathematica can read it?
 
  • #4
The square brackets are a problem. If you could export without them, then
Code:
Import["k.txt", "CSV"]
should work.
 
  • Like
Likes member 428835 and pbuk
  • #5
DrClaude said:
The square brackets are a problem. If you could export without them
Python:
# use
output.write(str(result)[1:-1])
# instead of
output.write(str(result))
 
  • Like
Likes member 428835 and DrClaude
  • #6
Wow, amazing! Thank you both so much. PF is a life saver. Gonna have to donate money now, because the help here is insane and I want it to keep running.
 
  • Like
Likes DrClaude

1. How do I import a python list from a .txt file into Mathematica?

To import a python list from a .txt file into Mathematica, you can use the Import function and specify the format as "Python". For example: Import["file.txt", "Python"]. This will import the python list as a Mathematica list.

2. Can I import a specific python list from a .txt file into Mathematica?

Yes, you can specify the specific list to import by using the Part function. For example, if the python list is named "mylist" in the .txt file, you can import it as: Import["file.txt", "Python"][[1]]. This will import only the first list in the .txt file.

3. What if my python list contains nested lists?

If your python list contains nested lists, Mathematica will automatically import them as nested lists as well. You can access the nested lists using the Part function and specifying the appropriate indices.

4. Can I modify the imported python list in Mathematica?

Yes, once the python list is imported into Mathematica, it becomes a regular Mathematica list and can be modified using standard Mathematica functions.

5. Are there any limitations to importing python lists from .txt files into Mathematica?

There may be some limitations depending on the size and complexity of the python list. Mathematica may have trouble importing very large or highly nested lists. If you encounter any issues, you can try breaking the list into smaller chunks or restructuring it before importing.

Similar threads

  • Programming and Computer Science
Replies
21
Views
544
  • Programming and Computer Science
Replies
7
Views
435
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
8
Views
882
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
5
Views
861
  • Programming and Computer Science
2
Replies
65
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
5
Views
9K
  • Programming and Computer Science
Replies
2
Views
21K
Back
Top