0

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! :)

3
  • So basically you want to plot all these 9 points in a 3D plot right? Commented Jan 29, 2018 at 4:51
  • Yes. :) The real file is large, the sample.csv is a sample. Commented Jan 29, 2018 at 4:52
  • I was stuck in the first step: import the csv file and turn it into python list. Commented Jan 29, 2018 at 4:54

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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]]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.