Paraview 不包括pandas。一个解决方案是从源代码编译您自己的 Paraview 发行版并包含 pandas。无论如何,这将是困难的方式。
他们进行了一些讨论以包含它,但是,我不知道它是否会完成:add pandas in pvpython
@Rivers 建议的替代方案是留在 Paraview (pvpython) 中并将您的数据转换为 numpy.array。然后,您可以对数据进行排序或/和将其导出到 (*.csv) 文件。此类解决方案的优势在于您可以留在 Paraview 中并构建宏(带有功能区中的按钮)自动执行您经常执行的任务。
第一个解决方案(我知道的最快)
import numpy as np
import paraview.simple as ps
from vtk.numpy_interface import dataset_adapter as dsa
def export_vtk_table_to_csv_v1(vtk_table, vtk_table_name, save_path):
"""
This function exports a vtk table to a (*.csv) file
Parameters
----------
vtk_table: vtkTable class
vtk table containing the simulation data for the asset
vtk_table_name:
vtk table name
save_path: str
path of the folder where the (.csv) file is saved
Returns
-------
"""
# Getting the number of columns and rows
nb_cols = vtk_table.GetNumberOfColumns()
nb_rows = vtk_table.GetNumberOfRows()
# Built a numpy array that will be exported later on
# +1 row to insert the column names
arr = np.zeros((nb_rows + 1, nb_cols), dtype='U255')
# Storing the columns names in a list (will be the first row)
for col_index in range(0, nb_cols):
col_name = vtk_table.GetColumnName(col_index)
arr[0, col_index] = col_name
for row_index in range(0, nb_rows):
arr[row_index + 1, col_index] = \
vtk_table.GetValue(row_index, col_index)
np.savetxt(save_path + vtk_table_name + '.csv', arr,
delimiter=";", fmt="%s")
第二种方法(较慢)
def export_vtk_table_to_csv_v2(vtk_table, vtk_table_name, save_path):
"""
This function exports a vtk table to a (*.csv) file
Parameters
----------
vtk_table: vtkTable class
vtk table containing the simulation data for the asset
vtk_table_name:
vtk table name
save_path: str
path of the folder where the (.csv) file is saved
Returns
-------
"""
nTable=dsa.WrapDataObject(vtk_table)
columns = nTable.RowData.keys()
nb_rows = vtk_table.GetNumberOfRows ()
rows = []
for x in range(nb_rows):
row = [nTable.RowData[col].GetValue(x) for col in columns]
rows.append(row)
arr = lists_to_structured_np_array(columns, rows_list, 'U255')
np.savetxt(save_path + vtk_table_name + '.csv', arr,
delimiter=";", fmt="%s")
def lists_to_structured_np_array(headers_list, data_lists, dtype_list):
"""
This function gather several lists of data into a np structured array.
Each list corresponds to a column of the array. The list of headers and of
dtypes is also required.
Parameters
----------
headers_lits : list
the list get a clean console display)
data_lists: list
list of lists. Each sub list contain one column data
dtype_list: list
list containing the dtypes to apply
Returns
-------
numpy.array
"""
# If the dtype_list is a simple dtype, it need to be turned to a list
# with same length as headers_list
if type(dtype_list) != list:
dtype_list = [dtype_list] * len(headers_list)
# Combine the dtype_list and headers_list into a list of tuples
dtype = [tuple([x, y]) for x, y in zip(headers_list, dtype_list)]
# Convert the data list to a list of tuples
data = [tuple(x) for x in data_lists]
# Create the numpy array
structuredArr = np.array(data, dtype=dtype)
return structuredArr