我發現了很多類似這樣的問題,這些問題解釋了如何添加在整個 csv 中重復的具有相同值的列。
我所擁有的有點不同。我有 randlist.txt,它看起來像這樣:
24830
4557
30795
19711
15481
42181
17331
32468
39411
30067
5124
35030
17776
有超過 40,000 個值。與此 allpeeps.csv 檔案中的行數相同,如下所示
institutiongroup,personid,iscomplete,recordcreateddate,recordupdateddate,lastknowninstitution,personinstitutions,eventprogrammeapplications,customdata,hereadyyear,trackedbyhesa,foundbyhesa,sourceid,nssec,disabilityinfo,yeargroup,yeargroupoffset,expectedheentryyear,lastknownphase,autocalculateheentryyear,cohortyear,recordstateenum,photopermission,howmanyindicsusepolar4,polar4q1,polar4q12,polar4rate,polar4entrants,polar4pop,howmanyindicsusepolar3,polar3ahe,polar4ahe,tundraquintile,imddecile,estdecile,idacidecile,nssec13,nssec47,nssec48,nssecnc
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",1,TRUE,21/03/2019,21/03/2019,," { institution: { institutionId: 9921, displaySummary: Eggbuckland Community College 140104 (#009921), name: Eggbuckland Community College, laestabNo: 8794185, ukprn: 10043058, urn: 140104, ucasCode: 11246, educationPhase: 2, educationPhaseName: Secondary, town: Plymouth, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-21T12:39:06 } ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,0 out of 4,N,N,32.7,140,425,0 out of 4,2,3,4,8,9,8,0.61,0.38,0.39,0.06
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",2,TRUE,21/03/2019,21/03/2019,," { institution: { institutionId: 9921, displaySummary: Eggbuckland Community College 140104 (#009921), name: Eggbuckland Community College, laestabNo: 8794185, ukprn: 10043058, urn: 140104, ucasCode: 11246, educationPhase: 2, educationPhaseName: Secondary, town: Plymouth, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-21T12:48:12 } ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,4 out of 4,Y,Y,19.6,105,545,4 out of 4,2,1,2,3,2,3,0.36,0.6,0.64,0.06
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",3,TRUE,21/03/2019,21/03/2019,," { institution: { institutionId: 9921, displaySummary: Eggbuckland Community College 140104 (#009921), name: Eggbuckland Community College, laestabNo: 8794185, ukprn: 10043058, urn: 140104, ucasCode: 11246, educationPhase: 2, educationPhaseName: Secondary, town: Plymouth, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-21T13:07:23 } ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,1 out of 4,N,Y,27.1,160,585,0 out of 4,4,2,3,7,6,8,0.52,0.46,0.48,0.07
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",4,TRUE,21/03/2019,21/03/2019,," { institution: { institutionId: 9921, displaySummary: Eggbuckland Community College 140104 (#009921), name: Eggbuckland Community College, laestabNo: 8794185, ukprn: 10043058, urn: 140104, ucasCode: 11246, educationPhase: 2, educationPhaseName: Secondary, town: Plymouth, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-21T13:07:24 } ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,2 out of 4,Y,Y,19.6,105,545,1 out of 4,2,1,3,7,4,8,0.54,0.44,0.46,0.07
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",5,FALSE,21/03/2019,21/03/2019,," { institution: { institutionId: 9921, displaySummary: Eggbuckland Community College 140104 (#009921), name: Eggbuckland Community College, laestabNo: 8794185, ukprn: 10043058, urn: 140104, ucasCode: 11246, educationPhase: 2, educationPhaseName: Secondary, town: Plymouth, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-21T13:07:24 } ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,,,,,,,,,,,,,,,,,
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",6,TRUE,21/03/2019,21/03/2019,," { institution: { institutionId: 9921, displaySummary: Eggbuckland Community College 140104 (#009921), name: Eggbuckland Community College, laestabNo: 8794185, ukprn: 10043058, urn: 140104, ucasCode: 11246, educationPhase: 2, educationPhaseName: Secondary, town: Plymouth, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-21T13:07:25 } ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,4 out of 4,Y,Y,20.8,145,705,4 out of 4,2,1,1,4,4,3,0.42,0.53,0.58,0.07
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",7,TRUE,21/03/2019,21/03/2019,," { institution: { institutionId: 9921, displaySummary: Eggbuckland Community College 140104 (#009921), name: Eggbuckland Community College, laestabNo: 8794185, ukprn: 10043058, urn: 140104, ucasCode: 11246, educationPhase: 2, educationPhaseName: Secondary, town: Plymouth, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-21T13:07:25 } ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,1 out of 4,N,Y,27.1,160,585,0 out of 4,2,2,2,6,5,7,0.49,0.48,0.51,0.06
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",8,FALSE,21/03/2019,21/03/2019,," { institution: { institutionId: 35023, displaySummary: IKB Academy 142127 (#035023), name: IKB Academy, laestabNo: 8004004, ukprn: 10053995, urn: 142127, ucasCode: 19579, educationPhase: 2, educationPhaseName: Secondary, town: Bristol, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-28T15:47:06 } ",System.Object[],{},Unknown,FALSE,FALSE,,0,,Unknown,0,,,TRUE,,0,,,,,,,,,,,,,,,,,,
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",9,FALSE,21/03/2019,21/03/2019,," { institution: { institutionId: 35023, displaySummary: IKB Academy 142127 (#035023), name: IKB Academy, laestabNo: 8004004, ukprn: 10053995, urn: 142127, ucasCode: 19579, educationPhase: 2, educationPhaseName: Secondary, town: Bristol, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-28T15:47:07 } ",System.Object[],{},Unknown,FALSE,FALSE,,0,,Unknown,0,,,TRUE,,0,,,,,,,,,,,,,,,,,,
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",10,FALSE,21/03/2019,21/03/2019,," { institution: { institutionId: 35023, displaySummary: IKB Academy 142127 (#035023), name: IKB Academy, laestabNo: 8004004, ukprn: 10053995, urn: 142127, ucasCode: 19579, educationPhase: 2, educationPhaseName: Secondary, town: Bristol, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-28T15:47:07 } ",System.Object[],{},Unknown,FALSE,FALSE,,0,,Unknown,0,,,TRUE,,0,,,,,,,,,,,,,,,,,,
我想要的輸出是
rand,institutiongroup,personid,iscomplete,recordcreateddate,recordupdateddate,lastknowninstitution,personinstitutions,eventprogrammeapplications,customdata,hereadyyear,trackedbyhesa,foundbyhesa,sourceid,nssec,disabilityinfo,yeargroup,yeargroupoffset,expectedheentryyear,lastknownphase,autocalculateheentryyear,cohortyear,recordstateenum,photopermission,howmanyindicsusepolar4,polar4q1,polar4q12,polar4rate,polar4entrants,polar4pop,howmanyindicsusepolar3,polar3ahe,polar4ahe,tundraquintile,imddecile,estdecile,idacidecile,nssec13,nssec47,nssec48,nssecnc
24830,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},1,TRUE,21/03/2019,21/03/2019,," { institution: { institutionId: 9921, displaySummary: Eggbuckland Community College 140104 (#009921), name: Eggbuckland Community College, laestabNo: 8794185, ukprn: 10043058, urn: 140104, ucasCode: 11246, educationPhase: 2, educationPhaseName: Secondary, town: Plymouth, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-21T12:39:06 } ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,0 out of 4,N,N,32.7,140,425,0 out of 4,2,3,4,8,9,8,0.61,0.38,0.39,0.06
4557,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},2,TRUE,21/03/2019,21/03/2019,," { institution: { institutionId: 9921, displaySummary: Eggbuckland Community College 140104 (#009921), name: Eggbuckland Community College, laestabNo: 8794185, ukprn: 10043058, urn: 140104, ucasCode: 11246, educationPhase: 2, educationPhaseName: Secondary, town: Plymouth, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-21T12:48:12 } ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,4 out of 4,Y,Y,19.6,105,545,4 out of 4,2,1,2,3,2,3,0.36,0.6,0.64,0.06
30795,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},3,TRUE,21/03/2019,21/03/2019,," { institution: { institutionId: 9921, displaySummary: Eggbuckland Community College 140104 (#009921), name: Eggbuckland Community College, laestabNo: 8794185, ukprn: 10043058, urn: 140104, ucasCode: 11246, educationPhase: 2, educationPhaseName: Secondary, town: Plymouth, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-21T13:07:23 } ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,1 out of 4,N,Y,27.1,160,585,0 out of 4,4,2,3,7,6,8,0.52,0.46,0.48,0.07
19711,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},4,TRUE,21/03/2019,21/03/2019,," { institution: { institutionId: 9921, displaySummary: Eggbuckland Community College 140104 (#009921), name: Eggbuckland Community College, laestabNo: 8794185, ukprn: 10043058, urn: 140104, ucasCode: 11246, educationPhase: 2, educationPhaseName: Secondary, town: Plymouth, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-21T13:07:24 } ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,2 out of 4,Y,Y,19.6,105,545,1 out of 4,2,1,3,7,4,8,0.54,0.44,0.46,0.07
15481,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},5,FALSE,21/03/2019,21/03/2019,," { institution: { institutionId: 9921, displaySummary: Eggbuckland Community College 140104 (#009921), name: Eggbuckland Community College, laestabNo: 8794185, ukprn: 10043058, urn: 140104, ucasCode: 11246, educationPhase: 2, educationPhaseName: Secondary, town: Plymouth, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-21T13:07:24 } ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,,,,,,,,,,,,,,,,,
42181,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},6,TRUE,21/03/2019,21/03/2019,," { institution: { institutionId: 9921, displaySummary: Eggbuckland Community College 140104 (#009921), name: Eggbuckland Community College, laestabNo: 8794185, ukprn: 10043058, urn: 140104, ucasCode: 11246, educationPhase: 2, educationPhaseName: Secondary, town: Plymouth, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-21T13:07:25 } ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,4 out of 4,Y,Y,20.8,145,705,4 out of 4,2,1,1,4,4,3,0.42,0.53,0.58,0.07
17331,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},7,TRUE,21/03/2019,21/03/2019,," { institution: { institutionId: 9921, displaySummary: Eggbuckland Community College 140104 (#009921), name: Eggbuckland Community College, laestabNo: 8794185, ukprn: 10043058, urn: 140104, ucasCode: 11246, educationPhase: 2, educationPhaseName: Secondary, town: Plymouth, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-21T13:07:25 } ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,1 out of 4,N,Y,27.1,160,585,0 out of 4,2,2,2,6,5,7,0.49,0.48,0.51,0.06
32468,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},8,FALSE,21/03/2019,21/03/2019,," { institution: { institutionId: 35023, displaySummary: IKB Academy 142127 (#035023), name: IKB Academy, laestabNo: 8004004, ukprn: 10053995, urn: 142127, ucasCode: 19579, educationPhase: 2, educationPhaseName: Secondary, town: Bristol, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-28T15:47:06 } ",System.Object[],{},Unknown,FALSE,FALSE,,0,,Unknown,0,,,TRUE,,0,,,,,,,,,,,,,,,,,,
39411,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},9,FALSE,21/03/2019,21/03/2019,," { institution: { institutionId: 35023, displaySummary: IKB Academy 142127 (#035023), name: IKB Academy, laestabNo: 8004004, ukprn: 10053995, urn: 142127, ucasCode: 19579, educationPhase: 2, educationPhaseName: Secondary, town: Bristol, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-28T15:47:07 } ",System.Object[],{},Unknown,FALSE,FALSE,,0,,Unknown,0,,,TRUE,,0,,,,,,,,,,,,,,,,,,
30067,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},10,FALSE,21/03/2019,21/03/2019,," { institution: { institutionId: 35023, displaySummary: IKB Academy 142127 (#035023), name: IKB Academy, laestabNo: 8004004, ukprn: 10053995, urn: 142127, ucasCode: 19579, educationPhase: 2, educationPhaseName: Secondary, town: Bristol, recordState: Open }, educationPhase: 2, priority: 0, recordCreatedDate: 2019-03-28T15:47:07 } ",System.Object[],{},Unknown,FALSE,FALSE,,0,,Unknown,0,,,TRUE,,0,,,,,,,,,,,,,,,,,,
到目前為止我最接近的是
$rands = Get-Content ./randlist.txt;$People = ./allpeeps.csv;$csv = @();for ($i = 0; $i -lt $rands.Count; $i ){$row = "" | Select rands,People;$row.rands = $rands[$i];$row.People = $People[$i];$csv = $row};$csv | Export-Csv ./newpeeps.csv -NoTypeInformation
但是,這會導致 csv 包含 2 列 - 一個看起來正確的“rand”,另一列稱為人員,并且所有資訊都集中在一個列中。我怎樣才能將此列添加到 csv?
uj5u.com熱心網友回復:
這可能會奏效,因為無需對現有 Csv 進行任何修改,您可以將其視為簡單的文本檔案,只需在新檔案 ( randlist.txt) 的行前加上逗號即可。
# the comma before `rand` is important, don't remove it!
$newData = , 'rand' (Get-Content .\randlist.txt -ReadCount 0)
$csv = Get-Content .\allpeeps.csv -ReadCount 0
& {
for($i = 0; $i -lt [math]::Max($newData.Count, $csv.Count); $i ) {
# `$csv[$i].TrimStart('"')` if you need to remove the leading double-quote
$newData[$i] ',' $csv[$i]
}
} | Set-Content .\newData.csv # Use a test file here before replacing the original Csv
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/489919.html
