如何通过包含空格和其他标点符号的名称引用CSV列?

h22fl7wq  于 2023-06-27  发布在  其他
关注(0)|答案(2)|浏览(145)

使用CSV形式的Google联系人并确保电子邮件值是小写的,电子邮件地址列的默认名称是“E-Mail 1 - Value”,因此只迭代该列。然而,标点符号引起了一个问题:

PS C:\Users\Nick\Desktop>
PS C:\Users\Nick\Desktop> $csv | Get-Member -MemberType Properties

   TypeName: CSV:System.Management.Automation.PSCustomObject

Name                 MemberType   Definition
----                 ----------   ----------
Additional Name      NoteProperty string Additional Name=
Additional Name Yomi NoteProperty string Additional Name Yomi=
Billing Information  NoteProperty string Billing Information=
Birthday             NoteProperty string Birthday=
Directory Server     NoteProperty string Directory Server=
E-mail 1 - Type      NoteProperty string E-mail 1 - Type=
E-mail 1 - Value     NoteProperty string E-mail 1 - Value=Conwayfp@Shaw.Ca
Family Name          NoteProperty string Family Name=Pauls
Family Name Yomi     NoteProperty string Family Name Yomi=
Gender               NoteProperty string Gender=
Given Name           NoteProperty string Given Name=
Given Name Yomi      NoteProperty string Given Name Yomi=
Group Membership     NoteProperty string Group Membership=
Hobby                NoteProperty string Hobby=
Initials             NoteProperty string Initials=
Language             NoteProperty string Language=
Location             NoteProperty string Location=
Maiden Name          NoteProperty string Maiden Name=
Mileage              NoteProperty string Mileage=
Name                 NoteProperty string Name=Conway
Name Prefix          NoteProperty string Name Prefix=
Name Suffix          NoteProperty string Name Suffix=
Nickname             NoteProperty string Nickname=
Notes                NoteProperty string Notes=
Occupation           NoteProperty string Occupation=
Phone 1 - Type       NoteProperty string Phone 1 - Type=Mobile
Phone 1 - Value      NoteProperty string Phone 1 - Value=2502034036
Phone 2 - Type       NoteProperty string Phone 2 - Type=
Phone 2 - Value      NoteProperty string Phone 2 - Value=
Photo                NoteProperty string Photo=
Priority             NoteProperty string Priority=
Sensitivity          NoteProperty string Sensitivity=
Short Name           NoteProperty string Short Name=
Subject              NoteProperty string Subject=
Yomi Name            NoteProperty string Yomi Name=

PS C:\Users\Nick\Desktop>
PS C:\Users\Nick\Desktop> $csv | ForEach-Object {$_.'E-mail 1 - Value'.(Get-Culture).TextInfo.ToLower($_.'E-mail 1 - Value')  }

它返回一个错误:

At line:1 char:24
+ ... ach-Object {$_.'E-mail 1 - Value'.(Get-Culture).TextInfo.ToLower($_.' ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At line:1 char:24
+ ... ach-Object {$_.'E-mail 1 - Value'.(Get-Culture).TextInfo.ToLower($_.' ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At line:1 char:24
PS C:\Users\Nick\Desktop>

我认为这意味着这个专栏没有被正确引用。* 简而言之,在这种情况下,空格是如何转义的?*
另见:
How to convert a specific CSV column to TitleCase with PowerShell from the REPL console?

vfwfrxfs

vfwfrxfs1#

$_.'E-mail 1 - Value'
这-即使用 * 引号 - 是引用包含特殊字符(如空格)的属性名称的正确方式。
您甚至可以使用 * 表达式**计算 * 属性名称,使用分组运算符(...);例如:

# -> 42
([pscustomobject] @{ 'Foo 1' = 42 }).('Foo' + ' ' + '1')

使用(...)来计算属性名是你在尝试时错误地做的:
$_.'E-mail 1 - Value'.(Get-Culture).TextInfo.ToLower(...)
它尝试使用Get-Culture调用 * 的(字符串化)输出作为属性名 ,然后尝试访问结果值的.TextInfo属性,并尝试调用.ToLower()方法。
因为以.(Get-Culture)开头的所有内容都计算为$null-由于不存在这样的属性-
method call * 失败,因为你不能调用$null上的方法。
您正在寻找的是 * 只是 *:

(Get-Culture).TextInfo.ToLower($_.'E-mail 1 - Value')

(To将结果赋回属性,将$_.'E-mail 1 - Value' =放在上面的表达式之前。)
也就是说,您可以获取当前区域性的文本格式信息,并调用其小写方法,从而传入感兴趣的属性值。
但是,请注意,您不需要根据该区域性规则将当前区域性显式引用为小写:直接在字符串 * 上调用.ToLower() * 就足够了:

$_.'E-mail 1 - Value'.ToLower()
6ovsh4lw

6ovsh4lw2#

它没有很好的文档记录,但是如果您足够努力地搜索,您会发现您可以将名称包含空格的成员引用为$foo."Member Name With Spaces"。然而,我认为这在一般原则上是一种不好的做法;如果我知道我将向PowerShell脚本或cmdlet传递CSV,我将努力使列名不包含空格或其他烦人字符。

相关问题