1

让我先说我是一个只有一点开发经验的系统管理员。如果这不是 stackoverflow 的问题,请随时移动/删除/指向正确的方向。

我正在尝试做的是读取符合RFC4180的csv 文件,并将每个值放入数组中,以便在脚本中进一步处理。下面是我能想到的最复杂但最合规的 csv 行,它可以工作,但这个脚本将面向客户,所以如果你能看一下并测试逻辑/向我展示其中的部分,我会很高兴我错过了一些东西。

如果可能的话,我也喜欢最佳实践指针。

这是一个摘录,基本上我从 CSV 文件中读取一行,并根据我对 RFC 4180 的理解对每个字符进行迭代,测试和应用不同的东西。chr(34) 代表双引号 ("),这是唯一的方法我可以找到在 VBScript 中进行比较。

If isFirstChar = True And thisChar = chr(34) Then
    'Mark it as in quotes, but don't print it, as it's not part of the text meant for the din file
    isInQuotes = True
    isFirstChar = False
ElseIf thisChar = chr(34) And isInQuotes = True Then
    If nextChar = chr(34) Then
    'Per RFC4108, "" is an escape sequence
    'Print it, jump up the old CharCounter to prevent double handling.
        CharCounter = CharCounter +1
        FieldData = FieldData & thisChar
        isFirstChar = False
    ElseIf nextChar = "," Then
    'It's the end of the field, we can set the isInQuotes to false so that the next char is handled as end of field
    isInQuotes = False
    isFirstChar = False
    Else
    'CSV isn't RFC4180 compliant
    WScript.Echo "This CSV isn't RFC4180 compliant, please fix the file or contact <redacted> for assistance."
    End If
ElseIf thisChar = "," Then
    If isInQuotes = False Then
    'End of Field, handle appropriately
        FieldCounter = FieldCounter + 1
        Redim Preserve FieldArray(FieldCounter)
        FieldArray(FieldCounter) = FieldData
        FieldData = ""
        isFirstChar = True
    Else
    'In quotes, handle as regular char
    FieldData = FieldData & thisChar
    isFirstChar = False
    End If
Else
    'Got all the way here, it's just a regular character! (We hope)
    FieldData = FieldData & thisChar
    isFirstChar = False
End If

下面是 CSV 记录中的一行示例:

"Luke ,, Pearson","Luke ""Lucky""","""111 ""Brown Mountain Cres",,"CO""OROY",QLD,4563,,1234567,1,1.11,N,AT FRONT GATE,0712345678,0.0022,2
4

0 回答 0