我有一个包含10000行的Excel文件,如下所示:
softwarename1, a, b, c, d, e, f, g, h, i softwarename2, f, e, r, g, u, o, d softwarename3, softwarename4, x
我想成为以下人物:
softwarename1 a softwarename1 b softwarename1 c softwarename1 d softwarename1 e softwarename1 f softwarename1 g softwarename1 h softwarename1 i softwarename2 f softwarename2 e softwarename2 r softwarename2 g softwarename2 u softwarename2 o softwarename2 d softwarename3 softwarename4 x softwarename4 ac softwarename4 gd softwarename4 d
目前我只知道如何导入CSV文件:
[[" ", " "], [" ", " "]]
任何帮助都会受到赞赏,因为我不知道如何开始。
答案 0 :(得分:0)
此处不需要import-csv
( - 我甚至认为csv是错误的存储格式,因为应该指定每列的afaik)。以下是替代单行:
Get-Content C:\tmp\ib20161016.csv |
ForEach {$s = $_.split(","); $s[1..($s.length-1)] | ForEach{$s[0] + " " + $_}}
编辑:这不会给出在该行中只有一个条目的极端情况。请参阅另一个答案。
答案 1 :(得分:0)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script language="javascript">
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
}
</script>
</head>
<body>
<div class="slideshow-container">
<div class="mySlides fade">
<div class="numbertext">1 / 5</div>
<img src="img/hispanic1.png" alt="dancers performing" style="width:100%" >
<div class="text">Hispanic dancers performing at the Hispanic Heritage Festival</div>
</div>
<div class="mySlides fade">
<div class="numbertext">2 / 5</div>
<img src="img/pinkribbon2.png" alt="pink ribbon" style="width:100%">
<div class="text">Come support Breast Cancer Awareness Month at Santa Fe</div>
</div>
<div class="mySlides fade">
<div class="numbertext">3 / 5</div>
<img src="img/bloodbus1.png" alt="Blood Drive Bus" style="width:100%">
<div class="text">Come donate blood for our Halloween Event!</div>
</div>
<div class="mySlides fade">
<div class="numbertext">4 / 5</div>
<img src="img/volunteer1.png" alt="Volunteers Pointing" style="width:100%">
<div class="text">Volunteer with a wonderful organization at our Volunteer Fair!</div>
</div>
<div class="mySlides fade">
<div class="numbertext">5 / 5</div>
<img src="img/clubrush1.png" alt="Girls rugby table" style="width:100%">
<div class="text">Come out and join a club at Club Rush!</div>
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<br>
<div style="text-align:center">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
<span class="dot" onclick="currentSlide(4)"></span>
<span class="dot" onclick="currentSlide(5)"></span>
</div>
</body>
</html>
/* Slideshow container */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
margin-top: -22px;
padding: 16px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
}
/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}
/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* The dots/bullets/indicators */
.dot {
cursor:pointer;
height: 13px;
width: 13px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
注意:我不确定最后3个值的来源,我假设$source = get-content C:\tmp\ib20161016.csv
$Output = foreach ($row in $source) {
$rowarray = $row.split(',').trim()
$softwarename = $rowarray[0]
$values = $rowarray[1..($rowarray.length - 1)]
if ($null -ne $values) {
foreach ($value in $values) {
"$softwarename $value"
}
} else {
$softwarename
}
}
$Output | Out-File C:\tmp\ib20161016.txt
应该是softwarename4, x
您不希望使用Import-CSV,因为您没有列标题。如果您这样做,它会将您的电子表格转换为属性将无序的对象。它不是真正的CSV,因为不同的列不匹配,它实际上只是一个txt文件。
答案 2 :(得分:0)
这是一个不同的,但我喜欢Powershell的好挑战。根据您的数据,以下内容将根据您的详细信息生成所需的结果:
$data = gc -Path "C:\Users\Iris Classon\Documents\data.csv"
$csv = ""
$nl = [Environment]::NewLine
$data | % {
$columns = $_.ToString().Split(',')
For ($i=1; $i -lt $columns.Length; $i++) {
$csv += "{0} {1}{2}" -f $columns[0],$columns[$i],$nl
}
}
Write-Host $csv
<强>结果:强>
softwarename1 a
softwarename1 b
softwarename1 c
softwarename1 d
softwarename1 e
softwarename1 f
softwarename1 g
softwarename1 h
softwarename1 i
softwarename2 f
softwarename2 e
softwarename2 r
softwarename2 g
softwarename2 u
softwarename2 o
softwarename2 d
softwarename3
softwarename4 x
这是一个功能相同但由于某些验证检查而不易出错的功能。
<#
.SYNOPSIS
Splits CSV in an unusual way..
.DESCRIPTION
.INPUTS
Source file, target file and delimiter
.OUTPUTS
.EXAMPLE
Split-CSV -SourceFile "C:\data.csv" -TargetFile "C:\data3.csv" -Delimiter " "
Split-CSV -SourceFile "C:\data.csv" -TargetFile "C:\data3.csv" -Delimiter ","
#>
function Split-CSV {
[CmdletBinding(SupportsShouldProcess,ConfirmImpact = "high")]
param(
[Parameter(Mandatory = $true,HelpMessage = "`t Please provide path")]
[ValidateNotNullOrEmpty()]
[string]$SourceFile,
[Parameter(Mandatory = $true,HelpMessage = "`t Please provide path")]
[ValidateNotNullOrEmpty()]
[string]$TargetFile,
[Parameter(Mandatory = $true,HelpMessage = "`t Please provide delimiter for new csv")]
[ValidateNotNullOrEmpty()]
[string]$Delimiter
)
$data = gc -Path $SourceFile
$csv = ""
$nl = [Environment]::NewLine
$data | % {
if ($_){
$columns = $_.ToString().Split(',')
if ($columns.Length -gt 1){
For ($i=1; $i -lt $columns.Length; $i++) {
$csv += "{0}{1}{2}{3}" -f $columns[0],$Delimiter,$columns[$i],$nl
}
}
}
}
$csv | Out-File $TargetFile
}
Split-CSV -SourceFile&#34; C:\ data.csv&#34; -TargetFile&#34; C:\ data3.csv&#34; -Delimiter&#34;,&#34;
产生与之前相同的结果,但使用逗号作为分隔符