gpt4 book ai didi

Scala split 于\n

转载 作者:行者123 更新时间:2023-12-05 04:05:02 26 4
gpt4 key购买 nike

我有一个包含以下内容的文本文件-

"\n\n\n\n\n\n\n\n\t\n\t\t\t\n\t\t\t\t\t\n\t\t\t\t\n\t\t\n\n\n\t\n\t\t\n\t\t\t\t
Hotline: +49 40-300 51 701\n\t\n\t\n\t
Languages\n\t\n\t\n\t\t\n\t\t\n\t\t
Travel plan \n\t\n\t\n\n\n\n\t\t\n\n\t\t\n\t\t\t\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\n\t\t\t\t\n\t\t\t\t\n\t\t\t\n\n\t\t\n\n\t\t\n\t\t\t\t
Book\t
Packages from € 59\n
\tAccommodation and arrival\n
\tMusical packages\n
\tMaritime packages\n\t
Hamburg for Families\n\t
Experience Hamburg & Culture\n\n\n\n\n\t
Hotels from € 24\n\t
Book online now!\n\t
Theme hotels\n\t
Hotels by location\n\t
Special Offers\n\t
Hotels from A-Z\n\t
Other accommodation\n\n\n\n\n\t
Tickets from € 8\n\tBook online now!\n\t
Musicals Hamburg\n\tHamburg maritime\n\t
Sightseeing tours & city walks\n\tMuseums & Exhibitions\n\tHamburg for Families\n\n\n\n\n\t
Hamburg CARD\n\tBook online now!\n\tAll benefits at a glance\n\tFrequently asked questions\n\n\n\n\n\t
Group trips\n\tBooking request\n\tHamburg Guides and theme walks\n\n\n\n\n\n\n\t\n\t\tOffer\n\n\t\t\n\n\t\t\n\n\t\t
Hamburg CARD\n\t\tFree travel by bus, rail and ferry (HVV) and up to 50% discount on more than 150 tourist...\n\n\t\n\t\n\t\t\n\t\t\t\n\t\t\t\t
from 10,50 EUR\n\t\t\t\n\t\t\n\n\t\n\n\n\n\n\n\n\tAttractions\tBest of Hamburg\n\t
Town Hall\n\tThe \"Michel\"\n\tSt. Pauli & Reeperbahn\n\t
Elbphilharmonie\n\tJungfernstieg\n\tMiniatur Wunderland\n\tTierpark Hagenbeck\n\t
All about the Alster\n\tBlankenese\n\n\n\n\n\tHamburg Maritime\n\t
Urbanshore Hamburg\n\tPort of Hamburg\n\tLandungsbrücken\n\tFish Market\n\tSpeicherstadt\n\tOn the Elbe\n\tHafenCity\n\tWillkomm-Höft\n\tÖvelgönne\n\n\n\n\n\tHistoric Hamburg\n\tThe Old Elbe Tunnel\n\t"

我想在\n 上拆分它。我试过了

string.split("\n")
string.split('\n')
string.split("""\n""")
string.split("\\n")

这些似乎都不起作用。我如何在 Scala 中完成它?

最佳答案

按\n 拆分,然后按\t,展平,然后删除空字符串。

var lines = Source.fromFile("/Users/rasika/Documents/example.txt").getLines.mkString

val result = lines.split("\\\\n").flatMap(_.split("\\\\t")).filter(_.nonEmpty).toList

结果

Hotline: +49 40-300 51 701
Languages
Travel plan
Book
Packages from € 59
Accommodation and arrival
Musical packages
Maritime packages
Hamburg for Families
Experience Hamburg & Culture
Hotels from € 24
Book online now!
Theme hotels
Hotels by location
Special Offers
Hotels from A-Z
Other accommodation
Tickets from € 8
Book online now!
Musicals Hamburg
Hamburg maritime
Sightseeing tours & city walks
Museums & Exhibitions
Hamburg for Families
Hamburg CARD
Book online now!
All benefits at a glance
Frequently asked questions
Group trips
Booking request
Hamburg Guides and theme walks
Offer
Hamburg CARD
Free travel by bus, rail and ferry (HVV) and up to 50% discount on more than 150 tourist...
from 10,50 EUR
Output exceeds cutoff limit.

关于Scala split 于\n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51826505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com