Parsing 如何使用解析方言从CSV读取一行?

Parsing 如何使用解析方言从CSV读取一行?,parsing,csv,rebol,Parsing,Csv,Rebol,我正在尝试使用PARSE将CSV行转换为Rebol块。很容易用开放代码编写,但和其他问题一样,我正在尝试学习方言在没有开放代码的情况下能做些什么 因此,如果一句话说: "Look, that's ""MR. Fork"" to you!",Hostile Fork,,http://hostilefork.com 那么我要的是街区: [{Look, that's "MR. Fork" to you!} {Hostile Fork} none {http://hostilefork.com}]

我正在尝试使用PARSE将CSV行转换为Rebol块。很容易用开放代码编写,但和其他问题一样,我正在尝试学习方言在没有开放代码的情况下能做些什么

因此,如果一句话说:

"Look, that's ""MR. Fork"" to you!",Hostile Fork,,http://hostilefork.com
那么我要的是街区:

[{Look, that's "MR. Fork" to you!} {Hostile Fork} none {http://hostilefork.com}]
需注意的事项:

  • CSV字符串中的嵌入引号用
  • 逗号可以在引号内,因此是文字的一部分,而不是列分隔符
  • 相邻的分隔逗号的列表示空字段
  • 不包含引号或逗号的字符串可以不带引号出现
  • 目前,我们可以保留
    http://rebol.com
    作为字符串!而不是将它们划分为以下类型:
为了使其更加统一,我要做的第一件事是在输入行中附加一个逗号。然后我有一个
列规则
,它捕获一个以逗号结尾的列…它可能在引号中,也可能不在引号中

我知道由于标题行,应该有多少列,因此代码会说:

unless parse line compose [(column-count) column-rule] [
    print rejoin [{Expected } column-count { columns.}]
]

但是我在写
列规则时有点卡住了。我需要一种方言来表达“一旦你找到一个引号,不断跳过引号对,直到你找到一个独立的引号。”这样做的好方法是什么?

与大多数解析问题一样,我尝试构建一种最能描述输入格式元素的语法

在这种情况下,我们有名词:

[comma ending value-chars qmark quoted-chars value header row]
一些动词:

[row-feed emit-value]
以及操作名词:

[current chunk current-row width]
我想我可以把它再细分一点,但这就足够了。首先,基金会:

comma: ","
ending: "^/"
qmark: {"}
value-chars: complement charset reduce [qmark comma ending]
quoted-chars: complement charset reduce [qmark]
现在是价值结构。引用的值是由有效字符或引号组成的块组成的,因为我们发现它们:

current: chunk: none
quoted-value: [
    qmark (current: copy "")
    any [
        copy chunk some quoted-chars (append current chunk)
        |
        qmark qmark (append current qmark)
    ]
    qmark
]

value: [
    copy current some value-chars
    | quoted-value
]

emit-value: [
    (
        delimiter: comma
        append current-row current
    )
]

emit-none: [
    (
        delimiter: comma
        append current-row none
    )
]
请注意,
分隔符
在每行的开头设置为
结束
,然后在传递值时立即更改为
逗号
。因此,输入行被定义为
[结束值任意[逗号值]]

剩下的就是定义文档结构:

current-row: none
row-feed: [
    (
        delimiter: ending
        append/only out current-row: copy []
    )
]

width: none
header: [
    (out: copy [])
    row-feed any [
        value comma
        emit-value
    ]
    value body: ending :body
    emit-value
    (width: length? current-row)
]

row: [
    row-feed width [
        delimiter [
            value emit-value
            | emit-none
        ]
    ]
]

if parse/all stream [header some row opt ending][out]
将其包装起来,以屏蔽所有这些文字,您将:

REBOL [
    Title: "CSV Parser"
    Date: 19-Nov-2012
    Author: "Christopher Ross-Gill"
]

parse-csv: use [
    comma ending delimiter value-chars qmark quoted-chars
    value quoted-value header row
    row-feed emit-value emit-none
    out current current-row width
][
    comma: ","
    ending: "^/"
    qmark: {"}
    value-chars: complement charset reduce [qmark comma ending]
    quoted-chars: complement charset reduce [qmark]

    current: none
    quoted-value: use [chunk][
        [
            qmark (current: copy "")
            any [
                copy chunk some quoted-chars (append current chunk)
                |
                qmark qmark (append current qmark)
            ]
            qmark
        ]
    ]

    value: [
        copy current some value-chars
        | quoted-value
    ]

    current-row: none
    row-feed: [
        (
            delimiter: ending
            append/only out current-row: copy []
        )
    ]
    emit-value: [
        (
            delimiter: comma
            append current-row current
        )
    ]
    emit-none: [
        (
            delimiter: comma
            append current-row none
        )
    ]

    width: none
    header: [
        (out: copy [])
        row-feed any [
            value comma
            emit-value
        ]
        value body: ending :body
        emit-value
        (width: length? current-row)
    ]

    row: [
        opt ending end break
        |
        row-feed width [
            delimiter [
                value emit-value
                | emit-none
            ]
        ]
    ]

    func [stream [string!]][
        if parse/all stream [header some row][out]
    ]
]

我几年前就不得不这么做了。 我已经更新了我的funcs来处理从那以后发现的所有案例。我希望它现在更坚固

请注意,它可以处理内部有换行符的字符串,但是:

  • 字符串中的换行符必须仅为LF
  • 记录之间的换行必须是CRLF
  • 您必须使用read/binary加载文件,这样Rebol不会自动转换换行符
  • (1.和2.是Excel给出的,例如)

    如果需要,我将相应的
    块设置为csv

    [编辑]确定,对应项(注意:所有字符串!将用双引号括起来,如果您想在结果中显示标题,则标题必须位于块的第一行):


    此外,请从BrianH的rebol.org上找到%csv tools.r脚本


    这段代码很棒。与R2和R3配合使用。

    答案的响应时间非常快,似乎(到目前为止)对我给出的古怪数据有效!嘿,谢谢!实际上,我确实需要一个
    块到csv
    来完成这项任务,因此,如果您想编辑答案以将其加入,这将使我不必编写它(尽管这两个选项中比较容易)。
    ; Conversion function from CSV format
    csv-to-block: func [
        "Convert a string of CSV formated data to a Rebol block. First line is header."
        csv-data [string!] "CSV data."
        /separator separ [char!] "Separator to use if different of comma (,)."
        /without-header "Do not include header in the result."
        /local out line start end this-string header record value data chars spaces chars-but-space
        ; CSV format information http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm
    ] [
        out: copy []
        separ: any [separ #","]
    
        ; This function handle replacement of dual double-quote by quote while copying substring
        this-string: func [s e] [replace/all copy/part s e {""} {"}]
        ; CSV parsing rules
        header: [(line: copy []) value any [separ value | separ (append line none)] (if not without-header [append/only out line])]
        record: [(line: copy []) value any [separ value | separ (append line none)] (append/only out line)]
        value: [any spaces data any spaces (append line this-string start end)]
        data: [start: some chars-but-space any [some spaces some chars-but-space] end: | #"^"" start: any [some chars | {""} | separ | newline] end: #"^""]
        chars: complement charset rejoin [ {"} separ newline]
        spaces: charset exclude { ^-} form separ
        chars-but-space: exclude chars spaces
    
        parse/all csv-data [header any [newline record] any newline end]
        out
    ]
    
    block-to-csv: func [
        "Convert a block of blocks to a CSV formated string." 
        blk-data [block!] "block of data to convert"
        /separator separ "Separator to use if different of comma (,)."
        /local out csv-string record value v
    ] [
        out: copy ""
        separ: any [separ #","]
        ; This function convert a string to a CSV formated one
        csv-string: func [val] [head insert next copy {""} replace/all replace/all copy val {"} {""} newline #{0A} ]
        record: [into [some [value (append out separ)]]]
        value: [set v string! (append out csv-string v) | set v any-type! (append out form v)]
    
        parse/all blk-data [any [record (remove back tail out append out crlf)]]
        out
    ]