我从mathematica生成a csv file,它看起来像这样:
with open('sample.csv','r') as f:
scsv =f.read()
print(scsv)
产量
"{-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}
我想把它变成python列表以获得3D情节,这是我的尝试:
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)
此代码产生:
[['{-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}']]
我不知道如何改进它,帮助! :)
答案 0 :(得分:1)
你去吧 -
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')
我确信可以优化cv中的点数创建,但这可以帮助您获得健康的开始。确保您安装matplotlib
- pip install matplotlib
答案 1 :(得分:0)
如果您的原始列表是a,其中a是
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}']]
然后,
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)
哪个应该给你输出:
[[[-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]]]