使用Python重新排序.csv文件



我有一个关于Paraview、Anaconda和Python3的问题。

简单地说,我想在Paraview中打开一个文件.vtu,获取它的data.csv并重新排序。问题是,当我用pvpython运行脚本时,它无法识别panda;当我用";python3.py";,它不识别视差。我需要熊猫以这种特定的方式重新排序,因为有些数字是用大写E.的科学记数法

这是我的代码:

from paraview import simple
import csv
import pandas
reader = simple.OpenDataFile("flow3.vtu")
writer = simple.CreateWriter("data0.csv", reader)
writer.FieldAssociation = "Points"
writer.UpdatePipeline()
with open('data0.csv') as csvfile:
rdr = csv.reader(csvfile)
# Pandas have to be used here to read the scientific notation
b = sorted(rdr, key=lambda x: x[16], reverse=False)
c = sorted(b, key=lambda x: x[15], reverse=False)
with open('data0.csv', 'w') as csvout:
wrtr = csv.writer(csvout)
wrtr.writerows(c)

非常感谢。

这似乎是环境问题。

使用Anaconda或Miniconda,您应该为您的项目创建一个特定的虚拟环境。默认情况下,名为";基本;创建。

以下是解决问题的方法。

为虚拟环境选择一个名称。说"csvenv";。

然后:

# Create the environment named "csvenv"
conda create --name csvenv
# Activate the environment
conda activate csvenv
# In this environment, install paraview and pandas
conda install -c conda-forge paraview
conda install -c conda-forge pandas

然后,在这个环境中,运行您的代码。

请参阅https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html有关使用conda的虚拟环境的详细信息。

Paraview不包括pandas。一个解决方案是从源代码中编译您自己的Paraview发行版,并包含panda。无论如何,那将是一条艰难的道路。

他们进行了一些讨论,以包括它,然而,我不知道是否会这样做:在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

最新更新