Haskell从文件中删除尾随和前导空格

时间:2017-03-29 14:06:58

标签: string haskell io

假设我有一个文件

mary had a little lamb
         It's fleece was white as snow    
Everywhere   
    the child went   
   The lamb, the lamb was sure to go, yeah    

我如何将该文件作为字符串读取,并删除尾随和前导空格?它可以是空格或制表符。删除空格后会打印出来:

mary had a little lamb
It's fleece was white as snow
Everywhere
the child went
The lamb, the lamb was sure to go, yeah

这是我目前所拥有的:

import Data.Text as T

readTheFile = do
    handle <- openFile "mary.txt" ReadMode
    contents <- hGetContents handle
    putStrLn contents
    hClose handle
    return(contents)

main :: IO ()
main = do
    file <- readTheFile
    file2 <- (T.strip file)
    return()

3 个答案:

答案 0 :(得分:5)

您的代码建议对Haskell有一些误解,所以让我们在解决问题之前先了解您的代码。

import Data.Text as T

你正在使用Text,太棒了!我建议你也使用读写Text类型的IO操作,而不是在String s(链接字符列表)上工作的前奏提供的操作。也就是import Data.Text.IO as T

readTheFile = do
    handle <- openFile "mary.txt" ReadMode
    contents <- hGetContents handle
    putStrLn contents
    hClose handle
    return(contents)

哦,嘿,使用hGetContents并手动打开和关闭文件可能容易出错。考虑readTheFile = T.readFile "mary.txt"

main :: IO ()
main = do
    file <- readTheFile
    file2 <- (T.strip file)
    return()

这里有两个问题。

发布一个请注意,您已使用strip,因为它是一个IO操作......但事实并非如此。我建议你学习更多关于IO和绑定(do notation)vs let-bound变量的知识。 strip计算类型为Text的新值,并且可能您希望对该值执行一些有用的操作,例如编写它。

问题二剥离整个文件不同于一次剥离每一行。我建议你阅读mathk的答案。

所以最后我想你想要:

-- Qualified imports are accessed via `T.someSymbol`
import qualified Data.Text.IO as T
import qualified Data.Text as T

-- Not really need as a separate function unless you want to also
-- put the stripping here too.
readTheFile :: IO T.Text
readTheFile = T.readFile "mary.txt"

-- First read, then strip each line, then write a new file.
main :: IO ()
main =
    do file <- readTheFile
       let strippedFile = T.unlines $ map T.strip $ T.lines file
       T.writeFile "newfile.txt" (T.strip strippedFile)

答案 1 :(得分:2)

以下是您正在寻找的解决方案:

import qualified Data.Text as T

main = do
  trimedFile <- (T.unlines . map T.strip . T.lines) <$> T.readFile "mary.txt"
  T.putStr trimedFile
来自strip

Data.Text正在开展工作。

答案 2 :(得分:0)

然后一次读取文件或处理文件一行

> intercalate " ".words $ "   The lamb, the lamb    was sure to go, yeah   "
"The lamb, the lamb was sure to go, yeah"

但是,没有参数的unwordsintercalate " "更好,而且都不必导入。