5

我正在尝试使用 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为 STRING!而不是LOAD将它们转换为诸如URL 之类的类型!

为了使它更统一,我做的第一件事是在输入行后面附加一个逗号。然后我有一个column-rule捕获以逗号结尾的单个列...可能在引号中或不在引号中。

由于标题行,我知道应该有多少列,所以代码然后说:

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

但我有点坚持写作column-rule。我需要一种方言来表达“一旦你找到一个引用,继续跳过引用对,直到你找到一个完全独立的引用。” 这样做的好方法是什么?

4

3 回答 3

3

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

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

[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
    )
]

请注意,在每一行的开头delimiter设置为,然后在我们传递一个值时立即更改为。因此,输入行定义为。endingcomma[ending value any [comma value]]

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

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]
    ]
]
于 2012-11-19T13:42:52.737 回答
2

几年前我不得不这样做。我已经更新了我的函数来处理我发现的所有案例。我希望它现在更坚固。

请注意,它可以在BUT内处理带有换行符的字符串:

  1. 字符串中的换行符只能是 LF并且...
  2. 记录之间的换行符必须是 CRLF...
  3. 您必须使用 read/binary 加载文件,以便 Rebol 不会自动转换换行符。

(例如,1. 和 2. 是 Excel 给出的)

; 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

[编辑]好的,对应的(注意:所有字符串!将用双引号括起来,如果您希望结果中出现标题,则标题必须位于块的第一行):

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
]
于 2012-11-20T08:52:31.050 回答
2

此外,在来自 BrianH 的 rebol.org 上找到 %csv-tools.r 脚本。

http://www.rebol.org/view-script.r?script=csv-tools.r

很棒的一段代码。适用于 R2 和 R3。

于 2012-12-06T16:42:01.143 回答