如何将嵌套列表中的每个列表分别写入文件?
How can I write every list from a nested list separately to a file?
我正在尝试将嵌套列表中的每个列表写入同一个文件。
但是我希望每个列表都以换行符开始。
我已经做了这个并且它可以工作,但是它非常低效并且看起来很糟糕:
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")
我试过类似下面的方法,但我似乎无法正确使用映射函数:
listValues :: [[String]] -> [String]
listValues :: (map . map)
appendValues :: [[String]] -> IO ()
appendValues = appendFile "scraped.txt" listValues
现在的txt文件是这样的,没关系,我只是想知道如何改进我的代码,学习如何使用映射功能。
Title,Subtitle,Date,Author
[["Een gezonde samenleving? 686Het belang van sporten wordt onderschat687","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 Oekra55ne","Statement van het CvB","07 maart 2022","HAN redactie"]]
要在 map-like 循环中执行此操作,您通常会使用 mapM_
library function。函数名称中的最后一个下划线_表示操作结果被忽略,这很适合我们,因为我们想要 IO ()
.
的结果类型
获取文件句柄似乎是合适的,以避免要求 OS 重复重新打开同一个输出文件,这是一种浪费。
可能的代码:
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
测试:
$
$ 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"]
$
我正在尝试将嵌套列表中的每个列表写入同一个文件。 但是我希望每个列表都以换行符开始。
我已经做了这个并且它可以工作,但是它非常低效并且看起来很糟糕:
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")
我试过类似下面的方法,但我似乎无法正确使用映射函数:
listValues :: [[String]] -> [String]
listValues :: (map . map)
appendValues :: [[String]] -> IO ()
appendValues = appendFile "scraped.txt" listValues
现在的txt文件是这样的,没关系,我只是想知道如何改进我的代码,学习如何使用映射功能。
Title,Subtitle,Date,Author
[["Een gezonde samenleving? 686Het belang van sporten wordt onderschat687","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 Oekra55ne","Statement van het CvB","07 maart 2022","HAN redactie"]]
要在 map-like 循环中执行此操作,您通常会使用 mapM_
library function。函数名称中的最后一个下划线_表示操作结果被忽略,这很适合我们,因为我们想要 IO ()
.
获取文件句柄似乎是合适的,以避免要求 OS 重复重新打开同一个输出文件,这是一种浪费。
可能的代码:
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
测试:
$
$ 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"]
$