Import python list from .txt file into Mathematica

Click For Summary

Discussion Overview

The discussion revolves around the process of importing a list of numbers from a .txt file created in Python into Mathematica. Participants explore methods for formatting the data correctly to facilitate this import.

Discussion Character

  • Technical explanation, Conceptual clarification, Debate/contested

Main Points Raised

  • One participant describes the issue of importing a list from a .txt file into Mathematica using ReadList, noting that it imports the entire list as a string.
  • Another participant asks for clarification on how the list is being exported from Python.
  • A participant shares their current method of exporting the list in Python, expressing uncertainty about how to format it for Mathematica.
  • Some participants suggest that the square brackets in the exported string may cause issues, proposing that removing them could allow for successful import using Import["k.txt", "CSV"].
  • One participant recommends modifying the export code to exclude the square brackets by using string slicing.
  • A later reply expresses gratitude for the assistance received, highlighting the value of the community's help.

Areas of Agreement / Disagreement

Participants generally agree that the square brackets are problematic for importing the data into Mathematica, and there is a consensus on the need to modify the export format. However, the discussion does not reach a definitive conclusion on the best method for exporting the list from Python.

Contextual Notes

The discussion does not address potential limitations related to the format of the data or the assumptions about how Mathematica interprets different file formats.

Who May Find This Useful

Individuals working with data transfer between Python and Mathematica, particularly those dealing with numerical lists in text files.

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
How are you exporting the list in Python?
 
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?
 
The square brackets are a problem. If you could export without them, then
Code:
Import["k.txt", "CSV"]
should work.
 
  • Like
Likes   Reactions: member 428835 and pbuk
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   Reactions: member 428835 and DrClaude
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   Reactions: DrClaude

Similar threads

Replies
35
Views
7K
  • · Replies 7 ·
Replies
7
Views
5K
Replies
12
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
Replies
5
Views
1K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
22K
  • · Replies 8 ·
Replies
8
Views
2K