【问题标题】:turn .csv file from mathematica into python list将.csv文件从mathematica转换成python列表
【发布时间】:2018-01-29 04:45:25
【问题描述】:

我有从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}']]

我不知道如何改进它,求助! :)

【问题讨论】:

  • 所以基本上你想在 3D 图中绘制所有这 9 个点对吗?
  • 是的。 :) 真实文件很大,sample.csv就是一个样本。
  • 卡在第一步:导入csv文件,转成python列表。

标签: python csv arraylist


【解决方案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

【讨论】:

    【解决方案2】:

    如果你的原始列表是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]]]
    

    【讨论】:

      猜你喜欢
      • 2016-01-15
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      • 2017-11-30
      • 2016-07-11
      • 2013-06-21
      • 2013-09-17
      相关资源
      最近更新 更多