Importing an irregular shaped array into PythonCalling an external command in PythonWhat are metaclasses in Python?Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?Does Python have a ternary conditional operator?Loop through an array in JavaScriptDoes Python have a string 'contains' substring method?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?
What does "Puller Prush Person" mean?
What does it mean to describe someone as a butt steak?
Why is Minecraft giving an OpenGL error?
infared filters v nd
Cross compiling for RPi - error while loading shared libraries
meaning of に in 本当に?
Java Casting: Java 11 throws LambdaConversionException while 1.8 does not
Does an object always see its latest internal state irrespective of thread?
Did Shadowfax go to Valinor?
Is it possible to run Internet Explorer on OS X El Capitan?
Perform and show arithmetic with LuaLaTeX
How do I deal with an unproductive colleague in a small company?
Why can't I see bouncing of a switch on an oscilloscope?
"You are your self first supporter", a more proper way to say it
NMaximize is not converging to a solution
Codimension of non-flat locus
Client team has low performances and low technical skills: we always fix their work and now they stop collaborate with us. How to solve?
Theorems that impeded progress
LWC SFDX source push error TypeError: LWC1009: decl.moveTo is not a function
Can a vampire attack twice with their claws using Multiattack?
Are the number of citations and number of published articles the most important criteria for a tenure promotion?
Can a Cauchy sequence converge for one metric while not converging for another?
What does the "remote control" for a QF-4 look like?
Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?
Importing an irregular shaped array into Python
Calling an external command in PythonWhat are metaclasses in Python?Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?Does Python have a ternary conditional operator?Loop through an array in JavaScriptDoes Python have a string 'contains' substring method?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have some data generated in Mathematica that I need imported into Python. The way the data is generated relies on symbolic calculations so simply generating it in Python is out of the question. The data is an array of dimensions (126,2) but, where the first position in each element is an integer, the second position is a list of lists and the dimensions are not constant from element to element, for example:
`
-9,4,2,5,6,8,10,
-2,3,6,6,1
4,3,6,6,1,3,6,6,1,3,6,6,1,3,6,6,1
`
would be the first three elements. The second position in each element will always a list of 2-D lists. The goal here is to have this data imported as a numpy array such that I can call each element, no matter it's position.
I had some success with numpy.genfromtxt("data.txt",delimiters="}}}")
which gives me the correct shape (126,2) but each element is simply "nan".
I had some more success with
`
with open("data.csv") as csvfile:
reader = csv.reader(csvfile, delimiter=' ')
for element in reader:
print(np.asarray(element)[0])
`
This gives me the integer values as an array, which is great! For the second position in each element i tried:
`
def replace_all(text, dic):
for i, j in dic.items():
text = text.replace(i, j)
return text
d="":"[","":"]"
with open("spinweights.csv") as csvfile:
reader = csv.reader(csvfile, delimiter=',')
it=0
for element in reader:
while it<1:
curlToSq=replace_all(str(element[1]),d)
print(np.asarray(curlToSq))
`
Where the replace_all
function is changing all curly brackets in square brackets (the thinking here was this would make it easier to convert into a numpy array). The final line there does return an array...of shape () with none of it's objects subscriptable, which is what I need!
Any help is appreciated.
python arrays numpy wolfram-mathematica
add a comment |
I have some data generated in Mathematica that I need imported into Python. The way the data is generated relies on symbolic calculations so simply generating it in Python is out of the question. The data is an array of dimensions (126,2) but, where the first position in each element is an integer, the second position is a list of lists and the dimensions are not constant from element to element, for example:
`
-9,4,2,5,6,8,10,
-2,3,6,6,1
4,3,6,6,1,3,6,6,1,3,6,6,1,3,6,6,1
`
would be the first three elements. The second position in each element will always a list of 2-D lists. The goal here is to have this data imported as a numpy array such that I can call each element, no matter it's position.
I had some success with numpy.genfromtxt("data.txt",delimiters="}}}")
which gives me the correct shape (126,2) but each element is simply "nan".
I had some more success with
`
with open("data.csv") as csvfile:
reader = csv.reader(csvfile, delimiter=' ')
for element in reader:
print(np.asarray(element)[0])
`
This gives me the integer values as an array, which is great! For the second position in each element i tried:
`
def replace_all(text, dic):
for i, j in dic.items():
text = text.replace(i, j)
return text
d="":"[","":"]"
with open("spinweights.csv") as csvfile:
reader = csv.reader(csvfile, delimiter=',')
it=0
for element in reader:
while it<1:
curlToSq=replace_all(str(element[1]),d)
print(np.asarray(curlToSq))
`
Where the replace_all
function is changing all curly brackets in square brackets (the thinking here was this would make it easier to convert into a numpy array). The final line there does return an array...of shape () with none of it's objects subscriptable, which is what I need!
Any help is appreciated.
python arrays numpy wolfram-mathematica
add a comment |
I have some data generated in Mathematica that I need imported into Python. The way the data is generated relies on symbolic calculations so simply generating it in Python is out of the question. The data is an array of dimensions (126,2) but, where the first position in each element is an integer, the second position is a list of lists and the dimensions are not constant from element to element, for example:
`
-9,4,2,5,6,8,10,
-2,3,6,6,1
4,3,6,6,1,3,6,6,1,3,6,6,1,3,6,6,1
`
would be the first three elements. The second position in each element will always a list of 2-D lists. The goal here is to have this data imported as a numpy array such that I can call each element, no matter it's position.
I had some success with numpy.genfromtxt("data.txt",delimiters="}}}")
which gives me the correct shape (126,2) but each element is simply "nan".
I had some more success with
`
with open("data.csv") as csvfile:
reader = csv.reader(csvfile, delimiter=' ')
for element in reader:
print(np.asarray(element)[0])
`
This gives me the integer values as an array, which is great! For the second position in each element i tried:
`
def replace_all(text, dic):
for i, j in dic.items():
text = text.replace(i, j)
return text
d="":"[","":"]"
with open("spinweights.csv") as csvfile:
reader = csv.reader(csvfile, delimiter=',')
it=0
for element in reader:
while it<1:
curlToSq=replace_all(str(element[1]),d)
print(np.asarray(curlToSq))
`
Where the replace_all
function is changing all curly brackets in square brackets (the thinking here was this would make it easier to convert into a numpy array). The final line there does return an array...of shape () with none of it's objects subscriptable, which is what I need!
Any help is appreciated.
python arrays numpy wolfram-mathematica
I have some data generated in Mathematica that I need imported into Python. The way the data is generated relies on symbolic calculations so simply generating it in Python is out of the question. The data is an array of dimensions (126,2) but, where the first position in each element is an integer, the second position is a list of lists and the dimensions are not constant from element to element, for example:
`
-9,4,2,5,6,8,10,
-2,3,6,6,1
4,3,6,6,1,3,6,6,1,3,6,6,1,3,6,6,1
`
would be the first three elements. The second position in each element will always a list of 2-D lists. The goal here is to have this data imported as a numpy array such that I can call each element, no matter it's position.
I had some success with numpy.genfromtxt("data.txt",delimiters="}}}")
which gives me the correct shape (126,2) but each element is simply "nan".
I had some more success with
`
with open("data.csv") as csvfile:
reader = csv.reader(csvfile, delimiter=' ')
for element in reader:
print(np.asarray(element)[0])
`
This gives me the integer values as an array, which is great! For the second position in each element i tried:
`
def replace_all(text, dic):
for i, j in dic.items():
text = text.replace(i, j)
return text
d="":"[","":"]"
with open("spinweights.csv") as csvfile:
reader = csv.reader(csvfile, delimiter=',')
it=0
for element in reader:
while it<1:
curlToSq=replace_all(str(element[1]),d)
print(np.asarray(curlToSq))
`
Where the replace_all
function is changing all curly brackets in square brackets (the thinking here was this would make it easier to convert into a numpy array). The final line there does return an array...of shape () with none of it's objects subscriptable, which is what I need!
Any help is appreciated.
python arrays numpy wolfram-mathematica
python arrays numpy wolfram-mathematica
asked Mar 9 at 0:00
Brendan ReidBrendan Reid
112
112
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I think converting this to a list structure will be easiest.
I added a ,
to the following:
In [22]: astr="""
...: -9,4,2,5,6,8,10,
...: -2,3,6,6,1,
...: 4,3,6,6,1,3,6,6,1,3,6,6,1,3,6,6,1
...: """
In [23]: astr1=astr.replace('','[').replace('',']').replace('n','')
In [24]: astr1
Out[24]: ' [ [-9,[[4,2],[5,6],[8,10]]], [-2,[[3,6],[6,1]]], [4,[[3,6],[6,1],[3,6],[6,1],[3,6],[6,1],[3,6],[6,1]]] ]'
The are various string evaluators. eval
is always available. ast
is a bit safer. json.loads(astr1)
also works.
In [25]: alist= eval(astr1)
In [26]: alist
Out[26]:
[[-9, [[4, 2], [5, 6], [8, 10]]],
[-2, [[3, 6], [6, 1]]],
[4, [[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]]]]
If it must be an array, do:
In [27]: arr = np.array(alist)
In [28]: arr
Out[28]:
array([[-9, list([[4, 2], [5, 6], [8, 10]])],
[-2, list([[3, 6], [6, 1]])],
[4,
list([[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]])]],
dtype=object)
This is (3,2). arr[:,0]
is an array of integers, but arr[:,1]
is an array of lists.
genfromtxt
as a default tries to convert the input to floats (what it can convert becomes nan
). It is intended for csv
- a neat table of numbers of strings, with equal number of columns for each row.
add a comment |
You may use Export
with either "JSON"
or "PythonExpression"
to export the data to a file. Either of these can be directly read by Python.
Below I use ExportString
to demonstrate.
With
x =
-9, 4, 2, 5, 6, 8, 10,
-2, 3, 6, 6, 1,
4, 3, 6, 6, 1, 3, 6, 6, 1, 3, 6, 6, 1, 3, 6, 6, 1
;
Then
ExportString[x, "JSON", "Compact" -> True]
[[-9,[[4,2],[5,6],[8,10]]],[-2,[[3,6],[6,1]]],[4,[[3,6],[6,1],[3,6],[6,1],[3,6],[6,1],[3,6],[6,1]]]]
or
ExportString[x, "PythonExpression"]
[[-9, [[4, 2], [5, 6], [8, 10]]], [-2, [[3, 6], [6, 1]]], [4, [[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]]]]
Wolfram Language (a.k.a. Mathematica) can import and export many formats with the two above being some of the Basic Formats it supports. There is generally no need to custom code consumption of output as there is normally a format available that the other platform can naively (or has a library to) read.
Hope this helps.
This is very helpful! Unfortunately I'm still having issues getting it into Python as an array with subscriptable elements. I have it imported as a numpy nd array (type checked) but its shape is (). Any ideas? Copying and pasting would work but...
– Brendan Reid
Mar 9 at 2:24
@BrendanReid Perhaps try exporting in the"MAT"
format and see if there is a Python library that will import that. Otherwise ask a question on how to import a JSON array into a Python array. You should get more answers to that question.
– Edmund
Mar 9 at 2:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072623%2fimporting-an-irregular-shaped-array-into-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think converting this to a list structure will be easiest.
I added a ,
to the following:
In [22]: astr="""
...: -9,4,2,5,6,8,10,
...: -2,3,6,6,1,
...: 4,3,6,6,1,3,6,6,1,3,6,6,1,3,6,6,1
...: """
In [23]: astr1=astr.replace('','[').replace('',']').replace('n','')
In [24]: astr1
Out[24]: ' [ [-9,[[4,2],[5,6],[8,10]]], [-2,[[3,6],[6,1]]], [4,[[3,6],[6,1],[3,6],[6,1],[3,6],[6,1],[3,6],[6,1]]] ]'
The are various string evaluators. eval
is always available. ast
is a bit safer. json.loads(astr1)
also works.
In [25]: alist= eval(astr1)
In [26]: alist
Out[26]:
[[-9, [[4, 2], [5, 6], [8, 10]]],
[-2, [[3, 6], [6, 1]]],
[4, [[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]]]]
If it must be an array, do:
In [27]: arr = np.array(alist)
In [28]: arr
Out[28]:
array([[-9, list([[4, 2], [5, 6], [8, 10]])],
[-2, list([[3, 6], [6, 1]])],
[4,
list([[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]])]],
dtype=object)
This is (3,2). arr[:,0]
is an array of integers, but arr[:,1]
is an array of lists.
genfromtxt
as a default tries to convert the input to floats (what it can convert becomes nan
). It is intended for csv
- a neat table of numbers of strings, with equal number of columns for each row.
add a comment |
I think converting this to a list structure will be easiest.
I added a ,
to the following:
In [22]: astr="""
...: -9,4,2,5,6,8,10,
...: -2,3,6,6,1,
...: 4,3,6,6,1,3,6,6,1,3,6,6,1,3,6,6,1
...: """
In [23]: astr1=astr.replace('','[').replace('',']').replace('n','')
In [24]: astr1
Out[24]: ' [ [-9,[[4,2],[5,6],[8,10]]], [-2,[[3,6],[6,1]]], [4,[[3,6],[6,1],[3,6],[6,1],[3,6],[6,1],[3,6],[6,1]]] ]'
The are various string evaluators. eval
is always available. ast
is a bit safer. json.loads(astr1)
also works.
In [25]: alist= eval(astr1)
In [26]: alist
Out[26]:
[[-9, [[4, 2], [5, 6], [8, 10]]],
[-2, [[3, 6], [6, 1]]],
[4, [[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]]]]
If it must be an array, do:
In [27]: arr = np.array(alist)
In [28]: arr
Out[28]:
array([[-9, list([[4, 2], [5, 6], [8, 10]])],
[-2, list([[3, 6], [6, 1]])],
[4,
list([[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]])]],
dtype=object)
This is (3,2). arr[:,0]
is an array of integers, but arr[:,1]
is an array of lists.
genfromtxt
as a default tries to convert the input to floats (what it can convert becomes nan
). It is intended for csv
- a neat table of numbers of strings, with equal number of columns for each row.
add a comment |
I think converting this to a list structure will be easiest.
I added a ,
to the following:
In [22]: astr="""
...: -9,4,2,5,6,8,10,
...: -2,3,6,6,1,
...: 4,3,6,6,1,3,6,6,1,3,6,6,1,3,6,6,1
...: """
In [23]: astr1=astr.replace('','[').replace('',']').replace('n','')
In [24]: astr1
Out[24]: ' [ [-9,[[4,2],[5,6],[8,10]]], [-2,[[3,6],[6,1]]], [4,[[3,6],[6,1],[3,6],[6,1],[3,6],[6,1],[3,6],[6,1]]] ]'
The are various string evaluators. eval
is always available. ast
is a bit safer. json.loads(astr1)
also works.
In [25]: alist= eval(astr1)
In [26]: alist
Out[26]:
[[-9, [[4, 2], [5, 6], [8, 10]]],
[-2, [[3, 6], [6, 1]]],
[4, [[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]]]]
If it must be an array, do:
In [27]: arr = np.array(alist)
In [28]: arr
Out[28]:
array([[-9, list([[4, 2], [5, 6], [8, 10]])],
[-2, list([[3, 6], [6, 1]])],
[4,
list([[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]])]],
dtype=object)
This is (3,2). arr[:,0]
is an array of integers, but arr[:,1]
is an array of lists.
genfromtxt
as a default tries to convert the input to floats (what it can convert becomes nan
). It is intended for csv
- a neat table of numbers of strings, with equal number of columns for each row.
I think converting this to a list structure will be easiest.
I added a ,
to the following:
In [22]: astr="""
...: -9,4,2,5,6,8,10,
...: -2,3,6,6,1,
...: 4,3,6,6,1,3,6,6,1,3,6,6,1,3,6,6,1
...: """
In [23]: astr1=astr.replace('','[').replace('',']').replace('n','')
In [24]: astr1
Out[24]: ' [ [-9,[[4,2],[5,6],[8,10]]], [-2,[[3,6],[6,1]]], [4,[[3,6],[6,1],[3,6],[6,1],[3,6],[6,1],[3,6],[6,1]]] ]'
The are various string evaluators. eval
is always available. ast
is a bit safer. json.loads(astr1)
also works.
In [25]: alist= eval(astr1)
In [26]: alist
Out[26]:
[[-9, [[4, 2], [5, 6], [8, 10]]],
[-2, [[3, 6], [6, 1]]],
[4, [[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]]]]
If it must be an array, do:
In [27]: arr = np.array(alist)
In [28]: arr
Out[28]:
array([[-9, list([[4, 2], [5, 6], [8, 10]])],
[-2, list([[3, 6], [6, 1]])],
[4,
list([[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]])]],
dtype=object)
This is (3,2). arr[:,0]
is an array of integers, but arr[:,1]
is an array of lists.
genfromtxt
as a default tries to convert the input to floats (what it can convert becomes nan
). It is intended for csv
- a neat table of numbers of strings, with equal number of columns for each row.
edited Mar 9 at 0:36
answered Mar 9 at 0:28
hpauljhpaulj
117k786160
117k786160
add a comment |
add a comment |
You may use Export
with either "JSON"
or "PythonExpression"
to export the data to a file. Either of these can be directly read by Python.
Below I use ExportString
to demonstrate.
With
x =
-9, 4, 2, 5, 6, 8, 10,
-2, 3, 6, 6, 1,
4, 3, 6, 6, 1, 3, 6, 6, 1, 3, 6, 6, 1, 3, 6, 6, 1
;
Then
ExportString[x, "JSON", "Compact" -> True]
[[-9,[[4,2],[5,6],[8,10]]],[-2,[[3,6],[6,1]]],[4,[[3,6],[6,1],[3,6],[6,1],[3,6],[6,1],[3,6],[6,1]]]]
or
ExportString[x, "PythonExpression"]
[[-9, [[4, 2], [5, 6], [8, 10]]], [-2, [[3, 6], [6, 1]]], [4, [[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]]]]
Wolfram Language (a.k.a. Mathematica) can import and export many formats with the two above being some of the Basic Formats it supports. There is generally no need to custom code consumption of output as there is normally a format available that the other platform can naively (or has a library to) read.
Hope this helps.
This is very helpful! Unfortunately I'm still having issues getting it into Python as an array with subscriptable elements. I have it imported as a numpy nd array (type checked) but its shape is (). Any ideas? Copying and pasting would work but...
– Brendan Reid
Mar 9 at 2:24
@BrendanReid Perhaps try exporting in the"MAT"
format and see if there is a Python library that will import that. Otherwise ask a question on how to import a JSON array into a Python array. You should get more answers to that question.
– Edmund
Mar 9 at 2:35
add a comment |
You may use Export
with either "JSON"
or "PythonExpression"
to export the data to a file. Either of these can be directly read by Python.
Below I use ExportString
to demonstrate.
With
x =
-9, 4, 2, 5, 6, 8, 10,
-2, 3, 6, 6, 1,
4, 3, 6, 6, 1, 3, 6, 6, 1, 3, 6, 6, 1, 3, 6, 6, 1
;
Then
ExportString[x, "JSON", "Compact" -> True]
[[-9,[[4,2],[5,6],[8,10]]],[-2,[[3,6],[6,1]]],[4,[[3,6],[6,1],[3,6],[6,1],[3,6],[6,1],[3,6],[6,1]]]]
or
ExportString[x, "PythonExpression"]
[[-9, [[4, 2], [5, 6], [8, 10]]], [-2, [[3, 6], [6, 1]]], [4, [[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]]]]
Wolfram Language (a.k.a. Mathematica) can import and export many formats with the two above being some of the Basic Formats it supports. There is generally no need to custom code consumption of output as there is normally a format available that the other platform can naively (or has a library to) read.
Hope this helps.
This is very helpful! Unfortunately I'm still having issues getting it into Python as an array with subscriptable elements. I have it imported as a numpy nd array (type checked) but its shape is (). Any ideas? Copying and pasting would work but...
– Brendan Reid
Mar 9 at 2:24
@BrendanReid Perhaps try exporting in the"MAT"
format and see if there is a Python library that will import that. Otherwise ask a question on how to import a JSON array into a Python array. You should get more answers to that question.
– Edmund
Mar 9 at 2:35
add a comment |
You may use Export
with either "JSON"
or "PythonExpression"
to export the data to a file. Either of these can be directly read by Python.
Below I use ExportString
to demonstrate.
With
x =
-9, 4, 2, 5, 6, 8, 10,
-2, 3, 6, 6, 1,
4, 3, 6, 6, 1, 3, 6, 6, 1, 3, 6, 6, 1, 3, 6, 6, 1
;
Then
ExportString[x, "JSON", "Compact" -> True]
[[-9,[[4,2],[5,6],[8,10]]],[-2,[[3,6],[6,1]]],[4,[[3,6],[6,1],[3,6],[6,1],[3,6],[6,1],[3,6],[6,1]]]]
or
ExportString[x, "PythonExpression"]
[[-9, [[4, 2], [5, 6], [8, 10]]], [-2, [[3, 6], [6, 1]]], [4, [[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]]]]
Wolfram Language (a.k.a. Mathematica) can import and export many formats with the two above being some of the Basic Formats it supports. There is generally no need to custom code consumption of output as there is normally a format available that the other platform can naively (or has a library to) read.
Hope this helps.
You may use Export
with either "JSON"
or "PythonExpression"
to export the data to a file. Either of these can be directly read by Python.
Below I use ExportString
to demonstrate.
With
x =
-9, 4, 2, 5, 6, 8, 10,
-2, 3, 6, 6, 1,
4, 3, 6, 6, 1, 3, 6, 6, 1, 3, 6, 6, 1, 3, 6, 6, 1
;
Then
ExportString[x, "JSON", "Compact" -> True]
[[-9,[[4,2],[5,6],[8,10]]],[-2,[[3,6],[6,1]]],[4,[[3,6],[6,1],[3,6],[6,1],[3,6],[6,1],[3,6],[6,1]]]]
or
ExportString[x, "PythonExpression"]
[[-9, [[4, 2], [5, 6], [8, 10]]], [-2, [[3, 6], [6, 1]]], [4, [[3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1], [3, 6], [6, 1]]]]
Wolfram Language (a.k.a. Mathematica) can import and export many formats with the two above being some of the Basic Formats it supports. There is generally no need to custom code consumption of output as there is normally a format available that the other platform can naively (or has a library to) read.
Hope this helps.
answered Mar 9 at 1:58
EdmundEdmund
319213
319213
This is very helpful! Unfortunately I'm still having issues getting it into Python as an array with subscriptable elements. I have it imported as a numpy nd array (type checked) but its shape is (). Any ideas? Copying and pasting would work but...
– Brendan Reid
Mar 9 at 2:24
@BrendanReid Perhaps try exporting in the"MAT"
format and see if there is a Python library that will import that. Otherwise ask a question on how to import a JSON array into a Python array. You should get more answers to that question.
– Edmund
Mar 9 at 2:35
add a comment |
This is very helpful! Unfortunately I'm still having issues getting it into Python as an array with subscriptable elements. I have it imported as a numpy nd array (type checked) but its shape is (). Any ideas? Copying and pasting would work but...
– Brendan Reid
Mar 9 at 2:24
@BrendanReid Perhaps try exporting in the"MAT"
format and see if there is a Python library that will import that. Otherwise ask a question on how to import a JSON array into a Python array. You should get more answers to that question.
– Edmund
Mar 9 at 2:35
This is very helpful! Unfortunately I'm still having issues getting it into Python as an array with subscriptable elements. I have it imported as a numpy nd array (type checked) but its shape is (). Any ideas? Copying and pasting would work but...
– Brendan Reid
Mar 9 at 2:24
This is very helpful! Unfortunately I'm still having issues getting it into Python as an array with subscriptable elements. I have it imported as a numpy nd array (type checked) but its shape is (). Any ideas? Copying and pasting would work but...
– Brendan Reid
Mar 9 at 2:24
@BrendanReid Perhaps try exporting in the
"MAT"
format and see if there is a Python library that will import that. Otherwise ask a question on how to import a JSON array into a Python array. You should get more answers to that question.– Edmund
Mar 9 at 2:35
@BrendanReid Perhaps try exporting in the
"MAT"
format and see if there is a Python library that will import that. Otherwise ask a question on how to import a JSON array into a Python array. You should get more answers to that question.– Edmund
Mar 9 at 2:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072623%2fimporting-an-irregular-shaped-array-into-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown