list - Haskell HXT 解析行和列并获取 [[String]] 而不是 [String]

标签 list haskell spreadsheet hxt

我正在尝试使用 HXT 解析 ods(libreoffice 电子表格)文件,但遇到问题。 在电子表格中,一行有许多单元格(全部名称为“cell”),并且电子表格有许多行(全部名称为 row)。 当我尝试获取单元格的文本时,代码将它们混合在一起,最终得到一整堆不按行分隔的单元格...

当尝试解析以下内容时:

<spreadsheet>
    <row>
       <cell> <p>ABC</p> </cell>
       <cell> <p>DEF</p> </cell>
       <cell> <p>GHI</p> </cell>
    </row>
    <row>
       <cell> <p>abc</p> </cell>
       <cell> <p>def</p> </cell>
       <cell> <p>ghi</p> </cell>
    </row>
    <row>
       <cell> <p>123</p> </cell>
       <cell> <p>456</p> </cell>
       <cell> <p>789</p> </cell>
    </row>
</spreadsheet>

使用代码:

import Text.XML.HXT.Core

play arg = do { results <- runX (processor arg) ; print results }
atTag x = getChildren >>> isElem >>> hasName x

processor filename =
    readDocument [withValidate no] filename >>>
    atTag "spreadsheet" >>>
    atTag "row" >>>
    atTag "cell" >>>
    atTag "p" >>>
    getChildren >>> getText

它给出 [ABC, DEF, GHI, abc, def, ghi, 123, 456, 789] 而我想要的是 [[ABC, DEF, GHI], [abc, def, ghi], [123, 456 ,789]]。

我做错了什么?

最佳答案

您可以使用listA在适当的点将结果收集到列表中:

import System.Environment (getArgs)
import Text.XML.HXT.Core

processor filename =
  readDocument [withValidate no] filename
    />  hasName "spreadsheet"
    />  hasName "row"
    >>> listA (getChildren >>> hasName "cell" /> hasName "p" /> getText)

main = fmap head getArgs >>= runX . processor >>= print

这将打印您想要的结果。

请注意,我使用提供的 />hasName 而不是您的 atTag,但如果您愿意,您可以轻松翻译回来坚持使用atTag

关于list - Haskell HXT 解析行和列并获取 [[String]] 而不是 [String],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13392210/

相关文章:

python - 如果某个键相同,则获取所有值的平均值

haskell - 如何将 [TExp a] 转换为 TExp [a],或者以编程方式将 FineTH 应用于多个值?

ruby-on-rails - Roo 电子表格上传 OLE2 签名无效

java - 如何使用 Jackson yaml 以大括号 ([ ]) 格式将 Java 列表打印到 Yaml 列表

python 3.x : Subtract Same Index from Sequential Lines

list - 如何在OCaml中检查下一个 'head'为空?

linux - 在 Ubuntu Server 12.04 上安装最新 Haskell 源时出错

haskell - 是什么导致我的程序只打印括号?

excel - 看似毫无意义的excel公式

excel - 特定交易的中位数