Power Query - 从列表中替换字符串中的多个子字符串

时间:2017-09-05 09:38:54

标签: powerquery excel-2016

我有一个包含类似短语的专栏,我需要将其清除,如:,!:!通过用空格替换它们(以避免连接单词)。

输入和输出示例:

" bla.blabla X&!:blablabla" 需要成为 " bla blabla X blablabla&# 34;

我找不到一个简单的简单解决方案!

source = data,  //which has Oldcol column with data to clean
CharsToRemove = { ".", "'", ":", ";", "!", "?", "<", ">", "{", "}", "[", "]", "'", "`", "^", """", "&", "-","/", "\", "|","(", ")", "," , "=", "+"}, 
//line below is where I'd like to get to, it is not working
Newcol = Table.AddColumn(source , "NEWCOL", each 
             Text.Replace([Oldcol], CharsToRemove , " ") ) 

我正在寻找最后一条工作线,基本上。

嵌套替换语句的选项有效但由于要替换的字符的实际列表很长,所以它很笨重,并且它发生在代码的许多部分中。 我可以把它变成这个功能,但是我想知道是否有办法避免这一切,只是有一个聪明的线条来完成这项工作?

提前谢谢

2 个答案:

答案 0 :(得分:6)

对于多个文本替换,您可以使用List.Accumulate,例如

let
    CharsToRemove = Text.ToList(".':;!?<>{}[]'`^""&-/\|(),=+"),
    Source = #table({"Oldcol"},{{"bla.blabla X&!:blablabla"}}),
    Cleaned = Table.TransformColumns(Source,{{"Oldcol", each List.Accumulate(CharsToRemove,_,(String,Remove) => Text.Replace(String,Remove," "))}})
in
    Cleaned

答案 1 :(得分:2)

对于多列,您可以使用Table.ReplaceValue。

let
    CharsToRemove = Text.ToList(".':;!?<>{}[]'`^""&-/\|(),=+"),
    Source = #table({"Oldcol1", "Oldcol2"},{{"bla.blabla X&!:blablabla","bla.blabla X&!:blablabla"}}),
    Cleaned = List.Accumulate(CharsToRemove,Source,(Table,Remove) => Table.ReplaceValue(Table,Remove," ",Replacer.ReplaceText,{"Oldcol1","Oldcol2"}))
in
    Cleaned