电源外壳 | csv 文件编辑 A 列中的每一行以设置最大字符数
$lower = Import-Csv "C:\\Users\\X\\Desktop\\U\\cvv.csv"
$lower | ForEach-Object {
src['A']=src['A'].str[:20].str.lower()
}
$lower |
Export-Csv -Path "C:\\Users\\X\\Desktop\\U\\cvv2.csv"
我尝试过这个方法,但是不起作用。
我希望如果超过 20 个字符就删除并将其匹配到最多 20 个。
$lower = Import-Csv "C:\\Users\\X\\Desktop\\U\\cvv.csv"
$lower | ForEach-Object {
src['A']=src['A'].str[:20].str.lower()
}
$lower |
Export-Csv -Path "C:\\Users\\X\\Desktop\\U\\cvv2.csv"
I tried this method, but it does not work.
I want that if it is over 20 characters to delete and match it to a maximum of 20.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正在混合使用 Python 和 PowerShell 语法。
您可能正在寻找这个:
但是,如果某些属性值有可能少于 20 个字符,则需要做更多的工作,即避免
.Substring() 出现异常
方法否则会抛出异常。以下是一个较短的替代方案,但如果许多输入字符串短于 20 个字符,则性能会很差,因为异常处理在性能方面代价高昂:
尝试 { $_.A.Substring(0, 20) } catch { $_.A }
在 PowerShell (Core) 7+ 中,您可以将
if
语句缩短为:$_.A.长度 -gt 20 ? $_.A.Substring(0, 20) : $_.A
可选阅读:比较各种子字符串提取方法的性能。
在 PowerShell 中提取子字符串的方法有多种,它们在详细程度和性能方面差异很大:
但这两个方面并不相关,事实上,在这种情况下,最冗长的方法速度最快。
从广义上讲,这些方法可分为:
.Substring()
方法-替换
运算符下面是基准测试的结果,它给出了粗略的感觉相对性能:
PowerShell 中的性能测量并不是一门精确的科学,结果取决于许多因素 - 尤其是主机硬件;低于平均 50 次运行的基准测试是为了获得更好的感觉,而感兴趣的是
Factor
列中反映的相对性能(1.00
反映最快时间,所有其他值都是该时间的倍数)。(最多)20 个字符的子字符串提取。对
1,000
字符串执行,其中一半比该字符串长,一半比该字符串短。重要:基准测试将
.Substring()
调用的条件解决方案与无条件并列-替换和数组切片解决方案,这会扭曲结果 - 为了比较真正的子字符串提取性能,后两种方法也需要修改为使用条件。
.Substring()
方法使用条件处理的原因是,它是一种必要 - 为了避免异常 - 而其他方法简洁,即不必使用条件。基准测试结果:
.Substring()
的方法是迄今为止最快的 - 除非与try
/catch
结合使用(异常处理的成本很高) )。? :
) 的执行效果与等效的if
语句大致相同。-replace
的解决方案速度慢 3-5 倍,大约是使用后视断言变体的两倍。try
/catch
的解决方案,慢了两个数量级。基准源代码:
要自己运行这些基准,您必须从 此要点。
假设您已查看链接的 Gist 源代码以确保其安全(我个人可以向您保证,但您应该始终检查),您可以直接安装它,如下所示:
It looks like you're mixing Python and PowerShell syntax.
You're probably looking for this:
However, if there's a chance that some property values have fewer than 20 characters, more work is needed, namely to avoid the exception that the
.Substring()
method would otherwise throw.The following is a shorter alternative, but will perform poorly if many of the input strings are shorter than 20 characters, because exception handling is expensive in terms of performance:
try { $_.A.Substring(0, 20) } catch { $_.A }
In PowerShell (Core) 7+, you can shorten the
if
statement to:$_.A.Length -gt 20 ? $_.A.Substring(0, 20) : $_.A
Optional reading: comparing the performance of various substring-extraction approaches.
There are several approaches to extracting substrings in PowerShell, and they vary widely with respect to verbosity and performance:
The two aspects aren't related, however, and, in fact, the most verbose approach is fastest in this case.
Broadly speaking, the approaches can be classified as:
.Substring()
method-replace
operatorBelow are the results of benchmarks, which give a rough sense of relative performance:
Performance measurements in PowerShell aren't an exact science, and the results depend on many factors - not least the host hardware; the benchmarks below average 50 runs to get a better sense, and it is the relative performance, reflected in the
Factor
column that is of interest (1.00
reflecting the fastest time, all other values being multiples of that).Substring extraction of (up to) 20 chars. is performed on
1,000
strings, half of which are longer than that, half of which are shorter.Important: The benchmarks juxtapose conditional solutions for
.Substring()
calls with unconditional-replace
and array-slicing solutions, which skews the results - to compare true substring-extraction performance, the latter two approaches need to be modified to use conditionals too..Substring()
approach is that it is a necessity there - in order to avoid exceptions - whereas the appeal of the other approaches is concision, i.e. not having to use conditionals.Benchmark results:
.Substring()
-based approaches are by far the fastest - except if combined withtry
/catch
(exception handling is expensive).? :
) performs about the same as the equivalentif
statement.-replace
-based solutions are slower by a factor of 3-5 with the capture-group variant, and about twice as slow as that with the variant that uses a look-behind assertion.try
/catch
, by two orders of magnitude.Benchmark source code:
To run these benchmarks yourself, you must download function
Time-Command
from this Gist.Assuming you have looked at the linked Gist's source code to ensure that it is safe (which I can personally assure you of, but you should always check), you can install it directly as follows:
我会亲自使用索引运算符
[]
与或超过所需的长度:
I would personally use the index operator
[ ]
in combination with the range operator..
:It would handle strings that are below or above the desired Length: