Power Query 无法将制表符识别为代码中 .txt 文件中的分隔符

Power Query does not recognize tab as a delimiter in .txt files in the code

这是我第一次post来这里,所以如果问题已经在某个地方得到回答或者我做错了什么,我提前道歉。问题总结:

我正在做一些光谱测量,我使用的软件的数据保存在数百个 .txt 文件中。所有文件的内容相同:第一列是波长,第二列是强度。列之间用制表符分开。想法是将所有这些 .txt 文件插入 Power Query 中,重新排列列,以便只有一列包含波长(因为所有测量值始终相同),其余列为强度(第二列)所有插入的文件。

因此,所需的输出应如下所示:

波长(第一个文件),强度(第一个文件),强度(第二个文件),强度(第三个文件),...,强度(最后一个文件)。

我找到了这个绝妙的解决方案,但问题是,如果列之间用逗号分隔,它就无法正常工作。我尝试更改代码以使其识别选项卡,但我尝试过的东西没有用。我昨天还发现了 Power Query,所以我完全是初学者。这是代码:

let
    Source = Folder.Files("C:\Users\xxxxx\Desktop\new"),


// Standard UI; step renamed
FilteredTxt = Table.SelectRows(Source, each [Extension] = ".txt"),

// Standard UI; step renamed
RemovedColumns = Table.RemoveColumns(FilteredTxt,{"Name", "Extension", "Date accessed", "Date modified", "Date created", "Attributes", "Folder Path"}),

// UI add custom column "FileContents" with formula Csv.Document([Content]); step renamed
AddedFileContents = Table.AddColumn(RemovedColumns, "FileContents", each Csv.Document([Content])),

// Standard UI; step renamed
RemovedBinaryContent = Table.RemoveColumns(AddedFileContents,{"Content"}),

// In the next 3 steps, temporary names for the new columns are created ("Column2", "Column3", etcetera)
// Standard UI: add custom Index column, start at 2, increment 1
#"Added Index" = Table.AddIndexColumn(RemovedBinaryContent, "Index", 2, 1),

// Standard UI: select Index column, Transform tab, Format, Add Prefix: "Column"
#"Added Prefix" = Table.TransformColumns(#"Added Index", {{"Index", each "Column" & Text.From(_, "en-US"), type text}}), //type text

// Standard UI:
#"Renamed Columns" = Table.RenameColumns(#"Added Prefix",{{"Index", "ColumnName"}}),
// Now we have the names for the new columns

// Advanced Editor: create a list with records with FileContents (tables) and ColumnNames (text) (1 list item (or record) per txt file in the folder)
// From this list, the resulting table will be build in the next step.
ListOfRecords = Table.ToRecords(#"Renamed Columns"),

// Advanced Editor: use List.Accumulate to build the table with all columns, 
// starting with Column1 of the first file (Table.FromList(ListOfRecords{0}[FileContents][Column1], each {_}),)
// adding Column2 of each file for all items in ListOfRecords.
BuildTable = List.Accumulate(ListOfRecords,
                             Table.FromList(ListOfRecords{0}[FileContents][Column1], each{_}),
                             (TableSoFar,NewColumn) => 
                              Table.ExpandTableColumn(Table.NestedJoin(TableSoFar, "Column1", NewColumn[FileContents], "Column1", "Dummy", JoinKind.LeftOuter), "Dummy", {"Column2"}, {NewColumn[ColumnName]})),
    #"Sorted Rows" = Table.Sort(BuildTable,{{"Column1", Order.Ascending}})
in
    #"Sorted Rows"

    //each {_}
    //Splitter.SplitTextByWhitespace

This is the output I get when I run the code:

如果我更改 .txt 文件的前五行,使列之间有一个逗号,我会得到:

The desired output (first five rows)

我试图用 Splitter 函数将 Table.FromList 行中的每个{_}更改到末尾,但它不起作用。

如果您能看一下代码,并提出需要更改的内容才能正常工作,我将不胜感激。

干杯!

修改您的代码以插入 #"Added Prefix2" 代码,如下所示

#"Added Prefix" = Table.TransformColumns(#"Added Index", {{"Index", each "Column" & Text.From(_, "en-US"), type text}}), //type text
#"Added Prefix2" = Table.TransformColumns(#"Added Prefix" , {{"FileContents", each Table.SplitColumn(_, "Column1", Splitter.SplitTextByEachDelimiter({"#(tab)"}, QuoteStyle.Csv, false), {"Column1", "Column2"})}}),
// Standard UI:
#"Renamed Columns" = Table.RenameColumns(#"Added Prefix2",{{"Index", "ColumnName"}}),

做类似的时候我更喜欢这个版本。更紧凑并保留源文件的文件名

let Source = Folder.Files("C:\directory\subdirectory"),
#"Filtered Rows" = Table.SelectRows(Source, each ([Extension] = ".txt")),
#"Added Custom1" = Table.AddColumn(#"Filtered Rows", "Custom", each Csv.Document(File.Contents([Folder Path]&"\"&[Name]),[Delimiter=",", Encoding=1252, QuoteStyle=QuoteStyle.None])),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom1", "Custom", {"Column1"}, {"Column1"}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Expanded Custom", "Column1", Splitter.SplitTextByEachDelimiter({"#(tab)"}, QuoteStyle.Csv, false), {"Column1", "Column2"}),
#"Removed Other Columns1" = Table.SelectColumns(#"Split Column by Delimiter",{"Name", "Column1", "Column2"}),
#"Pivoted Column" = Table.Pivot(#"Removed Other Columns1", List.Distinct(#"Removed Other Columns1"[Name]), "Name", "Column2")
in  #"Pivoted Column"