【问题标题】:Reorder a .csv file with Python使用 Python 重新排序 .csv 文件
【发布时间】:2020-11-17 18:20:36
【问题描述】:

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

简单地说,我想在 Paraview 中打开一个 file.vtu,获取它的 data.csv 并重新排序。问题是当我用 pvpython 运行脚本时,它不能识别 pandas;当我使用“python3 .py”运行它时,它无法识别 paraview。 我需要 pandas 以这种特定方式重新排序,因为有些数字是大写 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)

非常感谢。

【问题讨论】:

  • ParaView 5.9.0 将包含熊猫。

标签: python pandas csv paraview anaconda3


【解决方案1】:

看来是环境问题。

使用 Anaconda 或 Miniconda,您应该为您的项目创建一个特定的虚拟环境。 默认情况下会创建一个名为“Base”的虚拟环境。

以下是解决问题的方法。

为您的虚拟环境选择一个名称。说“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

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

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

【讨论】:

    【解决方案2】:

    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
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-18
      • 2016-01-05
      • 2015-08-28
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 2021-12-27
      • 1970-01-01
      相关资源
      最近更新 更多