将CSV中的列与EXL中的列进行比较,并将结果添加到我的CSV文件中

时间:2018-02-09 22:37:00

标签: powershell

我想将我的CSV文件与EXL文件进行比较。

EXL文件有许多列,例如USERID和WAVE。我的CSV文件只有USERID列。

它应该将我的CSV列USerID与Exl中的USERID列进行比较,并检查CSV中用户A在EXL文件中的波形,并在CSV文件中添加一个名为" WAVE的波列并写入相关的波数到CSV文件中的用户A。

如果在EXL文件中找不到CSV文件中的名称,请写入列波形“未找到"。

我不确定我写了什么"从Excel中读取和获取值"是否有帮助,但我不知道如何/进一步做什么。

results() {
cat << EOF

Grade results . . .
Student name  : $firstname $lastname
Total points  : $sum
Course average: $average  ($score)

Course grade  : ${ltrgrades[$1]}

${prefixes[$1]} '${ltrgrades[$1]}' represents ${comments[$1]} course work.
EOF
}

更新

我以这种方式更改了脚本,但它没有给我任何错误或任何结果。 EXL文件有26000个原始文件。我不知道在我的剧本中是否需要花费太多时间或者是错误的。

$TXTFile = "C:\A.txt" 
$CSVFile = "C:\B.csv" 
$EXLFIle = "C:\C.xlsx"

#find all CNs in TXT file and list them in CSV File
Select-String -Path $TXTFile  -Pattern 'CN=(.*?),' -AllMatches |
  Select-Object -Expand Matches |
  ForEach-Object { $_.Groups[1].Value } | 
  select @{L="UserID"; E={$_}} |
  Export-CSV $CSVFile -noTypeInformation

# Read and Get Values from Excel 
#Create an instance of Excel.Application and Open Excel file
$ObjExcel = New-Object -ComObject Excel.Application
$Workbook = $ObjExcel.Workbooks.Open($EXLFIle)
$Sheet = $workbook.Worksheets.Item($SheetName)
$ObjExcel.Visible = $false

#Count max Rows
$RowMax = ($sheet.UsedRange.rows).count

#Declare the starting positions
$rowUserID,$colUserID = 1,2
$rowWave,$colWave = 1,9

for ($i=1; $i -le $RowMax-1; $i++)
{
$UserID = $Sheet.Cells.Item($rowUserID+$i,$colUserID).text
$Wave = $Sheet.Cells.Item($rowWave+$i,$colWave).text

Write-Host ("USERID: "+$UserID)
Write-Host ("Wave: "+$Wave)
}

$objExcel.quit() 

1 个答案:

答案 0 :(得分:0)

不要在开头导出用户ID,只需将它们存储在数组中,比如说from kivy.app import App from kivy.uix.label import Label from kivy.uix.gridlayout import GridLayout from kivy.graphics import Color, Rectangle from kivy.clock import Clock from kivy.core.window import Window class LabelX(Label): def set_bgcolor(self,r,b,g,o,*args): self.canvas.after.clear() with self.canvas.after: Color(r,g,b,o) Rectangle(pos=self.pos,size=self.size) class MyGrid(GridLayout): def __init__(self,cols,rows,**kwargs): super(MyGrid,self).__init__(**kwargs) self.cols = cols for i in range(rows): for j in range(cols): l = LabelX(text=str(i)+","+str(j)) l.rowcol = (i,j) self.add_widget(l) class GridApp(App): def build(self): self.g = MyGrid(8,8) Window.bind(size=self.checkerboard) return self.g def checkerboard(self, *args): for l in self.g.children: count = l.rowcol[0] + l.rowcol[1] if count % 2: l.set_bgcolor(1, 0, 0, 1) else: l.set_bgcolor(0, 0, 0, 1 ) if __name__=="__main__": app = GridApp() Clock.schedule_once(app.checkerboard, 1) app.run() ,输出创建一个空数组$UserList。现在在循环中,您可以进行比较并将用户和wave信息添加到$out=@()

$out

最终导出结果:

if($UserList -contains $UserID) {
    $w = $Wave
} else {
    $w = "NOT Found"
}
$out += new-object PSObject -Property @{"UserID"=$UserID; "Wave"=$w}