turn .csv file from mathematica into python list

249
January 29, 2018, at 10:32 AM

I have a csv file generated from mathematica, it look like this:

 with open('sample.csv','r') as f:
        scsv =f.read()
        print(scsv)

yields

"{-955.1999999999999, 1.5568236482421087, -0.03326937763412006}","{-955.1999999999999, 1.5568572873672764, -0.026663002665836356}","{-955.1999999999999, 1.5568909480671234, -0.01847982437149327}"
"{-950.4, 1.5568236482421087, -0.016625954967908727}","{-950.4, 1.5568572873672764, -0.001015311835489717}","{-950.4, 1.5568909480671234, 0.006326172704000158}"
"{-945.5999999999999, 1.5568236482421087, -0.04292903732414247}","{-945.5999999999999, 1.5568572873672764, -0.01602757944255171}","{-945.5999999999999, 1.5568909480671234, -0.014847744429619007}

I want to turn it into python list to get a 3D plot and this is my try:

try:
    # for Python 2.x
    from StringIO import StringIO
except ImportError:
    # for Python 3.x
    from io import StringIO
import csv
with open('sample.csv','r') as f:
    scsv =f.read()
    g = StringIO(scsv)
    reader = csv.reader(g,delimiter=',')
    your_list = list(reader)
    for row in reader:
        print('\t'.join(row))
print(your_list)

This code yields:

[['{-955.1999999999999, 1.5568236482421087, -0.03326937763412006}', '{-955.1999999999999, 1.5568572873672764, -0.026663002665836356}', '{-955.1999999999999, 1.5568909480671234, -0.01847982437149327}'], ['{-950.4, 1.5568236482421087, -0.016625954967908727}', '{-950.4, 1.5568572873672764, -0.001015311835489717}', '{-950.4, 1.5568909480671234, 0.006326172704000158}'], ['{-945.5999999999999, 1.5568236482421087, -0.04292903732414247}', '{-945.5999999999999, 1.5568572873672764, -0.01602757944255171}', '{-945.5999999999999, 1.5568909480671234, -0.014847744429619007}']]

I don't know how to improve it, help! :)

Answer 1

Here you go -

from io import StringIO
import csv
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
x = []
y = []
z = []
with open('sample.csv','r') as f:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        for elem in row:
            point = elem.replace('{','').replace('}','').split(',')
            point = [float(each_point) for each_point in point]
            x.append(point[0])
            y.append(point[1])
            z.append(point[2])
ax = plt.axes(projection='3d')
ax.scatter3D(x, y, z, c=z, cmap='Greens')

I am sure the points creation from the cv can be optimized, but this would get you to a healthy start. Make sure you install matplotlib - pip install matplotlib

Answer 2

if your original list is a, where a is

a = [['{-955.1999999999999, 1.5568236482421087, -0.03326937763412006}', '{-955.1999999999999, 1.5568572873672764, -0.026663002665836356}', '{-955.1999999999999, 1.5568909480671234, -0.01847982437149327}'], ['{-950.4, 1.5568236482421087, -0.016625954967908727}', '{-950.4, 1.5568572873672764, -0.001015311835489717}', '{-950.4, 1.5568909480671234, 0.006326172704000158}'], ['{-945.5999999999999, 1.5568236482421087, -0.04292903732414247}', '{-945.5999999999999, 1.5568572873672764, -0.01602757944255171}', '{-945.5999999999999, 1.5568909480671234, -0.014847744429619007}']]

Then,

b = []
for aa in a:
    c = []
    for aaa in aa:
        words = aaa.split(',')
        x = words[0].split('{')[0]
        y = words[1]
        z = words[2].split('}')[0]
        c.append([float(x), float(y), float(z)])
    b.append(c)

Which should give you the output:

 [[[-955.1999999999999, 1.5568236482421087, -0.03326937763412006],
  [-955.1999999999999, 1.5568572873672764, -0.026663002665836356],
  [-955.1999999999999, 1.5568909480671234, -0.01847982437149327]],
 [[-950.4, 1.5568236482421087, -0.016625954967908727],
  [-950.4, 1.5568572873672764, -0.001015311835489717],
  [-950.4, 1.5568909480671234, 0.006326172704000158]],
 [[-945.5999999999999, 1.5568236482421087, -0.04292903732414247],
  [-945.5999999999999, 1.5568572873672764, -0.01602757944255171],
  [-945.5999999999999, 1.5568909480671234, -0.014847744429619007]]]
Rent Charter Buses Company
READ ALSO
Kinect v2 I have a video .xef recorded. Now I want to extract the features from it to a file?

Kinect v2 I have a video .xef recorded. Now I want to extract the features from it to a file?

I am working on kinectv2 projectI want to extract the skeleton features from the

249
Web scraping: IndexError: list index out of range

Web scraping: IndexError: list index out of range

I am new to python and web-scrapingi have been trying to pull some data from a website

253
How to loop through a class instance?

How to loop through a class instance?

I was asked this questionGiven the code:

234
install pydub for windows with python 3.5

install pydub for windows with python 3.5

I have to convert 'wav' to 'flac'I guess there were some module that can use for this work

507