简体   繁体   English

如何将嵌套列表中的每个列表分别写入文件?

[英]How can I write every list from a nested list separately to a file?

I am trying to write every list from a nested list to the same file.我正在尝试将嵌套列表中的每个列表写入同一个文件。 However I want every list to start on a newline.但是我希望每个列表都以换行符开头。

I've made this and it works, but it's very inefficient and looks bad:我做了这个并且它有效,但它非常低效并且看起来很糟糕:

appendFile "scraped.txt" (show (take 1 l))
appendFile "scraped.txt" ("\n")
let l3 = (drop 1 l)
appendFile "scraped.txt" (show (take 1 l3))
appendFile "scraped.txt" ("\n")
let l4 = (drop 1 l3)
appendFile "scraped.txt" (show (take 1 l4))
appendFile "scraped.txt" ("\n")
let l5 = (drop 1 l4)
appendFile "scraped.txt" (show (take 1 l5))
appendFile "scraped.txt" ("\n")
let l6 = (drop 1 l5)
appendFile "scraped.txt" (show (take 1 l6))
appendFile "scraped.txt" ("\n")

I tried something like the following, but I can't seem to correctly use the mapping function:我尝试了类似下面的操作,但我似乎无法正确使用映射 function:

listValues :: [[String]] -> [String]
listValues :: (map . map)

appendValues :: [[String]] -> IO ()
appendValues = appendFile "scraped.txt" listValues

The txt file now looks like this, which is ok, I just want to know how I can improve my code and learn how to use the mapping function. txt 文件现在看起来像这样,没关系,我只是想知道如何改进我的代码并学习如何使用映射 function。

Title,Subtitle,Date,Author
[["Een gezonde samenleving? \226\128\156Het belang van sporten wordt onderschat\226\128\157","Teamsport","16 maart 2022","HAN redactie"]]
[["Zo vader, zo dochter","Carsten en Kirsten","10 maart 2022","HAN redactie"]]
[["Milieuvriendelijk vervoer met waterstof","Kennisclip","09 maart 2022","HAN redactie"]]
[["\"Ik heb zin in wat nog komen gaat\"","Master Mind","08 maart 2022","HAN redactie"]]
[["Oorlog in Oekra\195\175ne","Statement van het CvB","07 maart 2022","HAN redactie"]]

To do this in a map-like loop, you would typically use the mapM_ library function .要在类似地图的循环中执行此操作,您通常会使用mapM_库 function The final underscore_ in the function name means that action results are ignored, something which suits us as we want a result type of IO () . function 名称中的最后一个下划线 _ 表示操作结果被忽略,这很适合我们,因为我们想要IO ()的结果类型。

It seems appropriate to get a file handle, in order to avoid asking the OS to repetitively reopen the same output file, which is wasteful.获取文件句柄似乎是合适的,以避免要求操作系统重复重新打开同一个 output 文件,这是一种浪费。

Possible code:可能的代码:

import System.IO

type FileName = String

writeNestedList :: Show a => FileName -> [[a]] -> IO ()
writeNestedList fileName xss =
    do
       fh <- openFile  fileName  WriteMode  -- get a file handle
       mapM_  ((hPutStrLn fh) . show)  xss
       hClose fh


main :: IO ()
main = do
   let  xss = [ ["ab","bc"] ,["de","ef"], ["gh","hi"] ]
   writeNestedList  "scraped.txt"  xss

Testing:测试:

$ 
$ ghc q71608762.hs  -o q71608762.x
[1 of 1] Compiling Main             ( q71608762.hs, q71608762.o )
Linking ./q71608762.x ...
$ 
$ q71608762.x
$ 
$ cat scraped.txt
["ab","bc"]
["de","ef"]
["gh","hi"]
$ 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM