Python Import python list from .txt file into Mathematica

AI Thread Summary
The discussion revolves around importing a list of numbers from a k.txt file into Mathematica. The initial approach using ReadList["k.txt", String] results in the entire list being imported as a string, complicating the extraction of numbers. A user suggests an alternative method for exporting the list from Python to Mathematica, highlighting that the square brackets in the output pose a problem. The recommended solution is to modify the Python export code to use output.write(str(result)[1:-1]), which removes the square brackets, allowing Mathematica to read the data correctly using Import["k.txt", "CSV"]. The user expresses gratitude for the assistance received, indicating a strong appreciation for the community's support.
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 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 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top