- Haskell Data Analysis Cookbook
- Nishant Shukla
- 250字
- 2021-12-08 12:43:39
Splitting a string on lines, words, or arbitrary tokens
Useful data is often interspersed between delimiters, such as commas or spaces, making string splitting vital for most data analysis tasks.
Getting ready
Create an input.txt
file similar to the following one:
$ cat input.txt first line second line words are split by space comma,separated,values or any delimiter you want
Install the split
package using Cabal as follows:
$ cabal install split
How to do it...
- The only function we will need is
splitOn
, which is imported as follows:import Data.List.Split (splitOn)
- First we split the string into lines, as shown in the following code snippet:
main = do input <- readFile "input.txt" let ls = lines input print $ ls
- The lines are printed in a list as follows:
[ "first line","second line" , "words are split by space" , "comma,separated,values" , "or any delimiter you want"]
- Next, we separate a string on spaces as follows:
let ws = words $ ls !! 2 print ws
- The words are printed in a list as follows:
["words","are","split","by","space"]
- Next, we show how to split a string on an arbitrary value using the following lines of code:
let cs = splitOn "," $ ls !! 3 print cs
- The values are split on the commas as follows:
["comma","separated","values"]
- Finally, we show splitting on multiple letters as shown in the following code snippet:
let ds = splitOn "an" $ ls !! 4 print ds
- The output is as follows:
["or any d","limit","r you want"]
推薦閱讀
- 手機安全和可信應用開發指南:TrustZone與OP-TEE技術詳解
- 黑客攻防從入門到精通(實戰秘笈版)
- Mastering Apache Spark 2.x(Second Edition)
- H5頁面設計:Mugeda版(微課版)
- Express Web Application Development
- Visual Basic程序設計教程
- Modern C++ Programming Cookbook
- Unity Character Animation with Mecanim
- Kotlin極簡教程
- C編程技巧:117個問題解決方案示例
- Oracle 12c從入門到精通(視頻教學超值版)
- HTML5移動前端開發基礎與實戰(微課版)
- Microsoft Exchange Server 2016 PowerShell Cookbook(Fourth Edition)
- 深入大型數據集:并行與分布化Python代碼
- 前端程序員面試算法寶典