在csv文件开头添加列

时间:2020-09-09 16:22:54

标签: powershell

使用此代码,我将前两列写到csv

import numpy as np
from scipy import linalg as la
import concurrent.futures

# In real code
#         various parameters are used to build the matrix function,
#         it is presumably not sparse

# Matrix with independent variable x
def matrix_function(x):
    # Define dimensions and pre-allocate space for matrix
    #dim = 100        # For quicker evaluation/testing
    dim = 1000        # For conveying the scale of the problem
    matrix_dimensions = [dim, dim]
    # The matrix is complex
    mat = np.zeros(matrix_dimensions, dtype=complex)
    for i in range(dim):
        for j in range(i,dim):
            mat[i,j] = x*np.random.rand(1) + np.random.rand(1)*1J
            # Making the matrix Hermitian
            mat[j,i] = np.conjugate( mat[i,j] )
    return mat
        
# 400 Arguments for the defined matrix function
args = np.arange(0,10,0.025)

# Parallelizing evaluation of 400 matrices
with concurrent.futures.ProcessPoolExecutor() as pool:
    evaluated_matrix_functions = pool.map( matrix_function, args )
    ''' This will hang,
             which is what tipped me off to the issue
                                          **not important to question
        eigsystem = pool.map( la.lapack.zheevd,
                              evaluated_matrix_functions
                              )
    '''
    pool.shutdown()
    
''' This will cause a memory overflow,
              depending on the size of the matrices
              and how many of them; even with 32GB memory

with concurrent.futures.ThreadPoolExecutor() as pool:
    eigsystem = pool.map( la.lapack.zheevd,
                          evaluated_matrix_functions
                          )
    pool.shutdown()
'''

# The code which I run, in serial,
#          but still uses all cores/threads my 2700x provides at full load
eigensystem_list = []
for matrix in evaluated_matrix_functions:
    eigensystem_list.append( la.lapack.zheevd(matrix) )
    
# The eigensystem_list is then used in later calculations

返回此结果:

$Hash = [ordered]@{}

foreach ($Property in $Properties) {
    $Hash[$Property] = (($New_Extracted_Data.$Property | Measure -Sum).Sum)/$NumberOfRowsToPick
}

[pscustomobject]$Hash | Export-Csv "C:\script\sum_$Server$Services.csv" -NoType -Force

之后,我将使用此代码附加3列:

rx_AVERAGE  rx_MAX  rx_MIN  tx_AVERAGE  tx_MAX  tx_MIN  
20538.44384 73426.92533 1886.881034 6355.250931 20152.27358 1817.357994

这将产生最终结果:

$csv = Import-Csv -Path "c:\script\sum_$Server$Services.csv"                 

foreach($row in $csv)
{                    
    $row | Add-Member -MemberType NoteProperty -Name "ServerName" -Value $Server -Force
    $row | Add-Member -MemberType NoteProperty -Name "Start Time" -Value $FirstTimeStamp -Force 
    $row | Add-Member -MemberType NoteProperty -Name "End Time" -Value $LastTimeStamp -Force
}

$csv | Export-Csv -Path "c:\script\sum_$Server$Services.csv" -NoType

最后添加的是新添加的列。

我需要在开头添加新添加的列,就像这样:

rx_AVERAGE  rx_MAX  rx_MIN  tx_AVERAGE  tx_MAX  tx_MIN  ServerName  Start Time  End Time
20538.44384 73426.92533 1886.881034 6355.250931 20152.27358 1817.357994 devas1.devad.myworldpanel.com   1597044600  1599633000

请让我知道如何在开头而不是结尾添加它们?

1 个答案:

答案 0 :(得分:0)

将属性放入$Hash字典中之前添加循环中计算出的属性:

$Hash = [ordered]@{
    ServerName   = $Server
    "Start Time" = $FirstTimeStamp
    "End Time"   = $LastTimeStamp
}
foreach ($Property in $Properties) {
    $Hash[$Property] = (($New_Extracted_Data.$Property | Measure -Sum).Sum)/$NumberOfRowsToPick
}
[pscustomobject]$Hash

您还可以使用$Hash.Insert()来挤压字典顶部的新条目:

$Hash = [ordered]@{}
foreach ($Property in $Properties) {
    $Hash[$Property] = (($New_Extracted_Data.$Property | Measure -Sum).Sum)/$NumberOfRowsToPick
}

$hash.Insert(0, 'ServerName', $Server)
$hash.Insert(1, 'Start Time', $FirstTimeStamp)
$hash.Insert(2, 'End Time', $LastTimeStamp)

[pscustomobject]$Hash

对于现有对象(即,如果这些步骤是通过单独的脚本完成的),唯一的方法是创建一个新对象,为方便起见,可以使用Select-Object

$csv |Select-Object -Property @('ServerName';'Start Time';'End Time';$Properties)
相关问题