我正在使用pipes-csv库读取csv文件。我想阅读第一行,稍后阅读其他内容。不幸的是在Pipes.Prelude.head函数返回之后。莫名其妙地关闭了管道。有没有办法先读取csv的头部,然后再阅读其余部分。使用管道从csv文件读取第一行-csv
import qualified Data.Vector as V
import Pipes
import qualified Pipes.Prelude as P
import qualified System.IO as IO
import qualified Pipes.ByteString as PB
import qualified Data.Text as Text
import qualified Pipes.Csv as PCsv
import Control.Monad (forever)
showPipe :: Proxy() (Either String (V.Vector Text.Text))() String IO b
showPipe = forever $ do
x::(Either String (V.Vector Text.Text)) <- await
yield $ show x
main :: IO()
main = do
IO.withFile "./test.csv"
IO.ReadMode
(\handle -> do
let producer = (PCsv.decode PCsv.NoHeader (PB.fromHandle handle))
headers <- P.head producer
putStrLn "Header"
putStrLn $ show headers
putStrLn $ "Rows"
runEffect (producer>->
(showPipe) >->
P.stdoutLn)
)
如果我们不先读头,我们可以看到整个CSV没有任何问题:
main :: IO()
main = do
IO.withFile "./test.csv"
IO.ReadMode
(\handle -> do
let producer = (PCsv.decode PCsv.NoHeader (PB.fromHandle handle))
putStrLn $ "Rows"
runEffect (producer>->
(showPipe) >->
P.stdoutLn)
)
P.next是我的情况知府解决方案。也感谢您展示showPipe的地图版本。 – yilmazhuseyin
同时用P.concat删除Either是非常好的技巧。 – yilmazhuseyin