从文件路径中获取扩展名

huangapple go评论55阅读模式
英文:

Get the extension from a filepath

问题

I need to test the extension of the imported file as to if it is CSV or XLSX.

I am trying:

FP = Text.AfterDelimiter([Path], ".", {0, RelativePosition.FromEnd}),
let 
   FilePath = Excel.CurrentWorkbook(){[Name="fp"]}[Content]{0}[TheFilePath],
   FP = Text.AfterDelimiter([FilePath], ".", {0, RelativePosition.FromEnd}),
   Source = Excel.Workbook(File.Contents(FilePath))
in Source

But I get the error:

There is an unknown identifier. Did you use the [field] shorthand for a _[field] outside of an 'each' expression?

I did try:

FP = Text.AfterDelimiter(_[FilePath], ".", {0, RelativePosition.FromEnd})

This did not correct the issue.

英文:

I need to test the extension of the imported file as to if it is CSV or XLSX

I am trying

FP = Text.AfterDelimiter([Path], ".", {0, RelativePosition.FromEnd}),
let 
   FilePath = Excel.CurrentWorkbook(){[Name="fp"]}[Content]{0}[TheFilePath],
   FP = Text.AfterDelimiter([FilePath], ".", {0, RelativePosition.FromEnd}),
   Source = Excel.Workbook(File.Contents(FilePath))
in Source

But I get the error

There is an unknown identifier. Did you use the [field] shorthand for a _[field] outside of an 'each' expression?

I did try

 FP = Text.AfterDelimiter(_[FilePath], ".", {0, RelativePosition.FromEnd})

This did not correct the issue

答案1

得分: 1

这是一个查找最后一个句点后面部分的代码:

let FilePath = Excel.CurrentWorkbook(){[Name="fp"]}[Content]{0}[TheFilePath],
    Extension = List.Last(Text.Split(FilePath, "."))
in Extension

从文件路径中获取扩展名

英文:

How about this, which finds the part after the last period?

let  FilePath = Excel.CurrentWorkbook(){[Name="fp"]}[Content]{0}[TheFilePath],
Extension=List.Last(Text.Split(FilePath,"."))
in Extension

从文件路径中获取扩展名

答案2

得分: 1

= Table.AddColumn(#"AddPathColumn", "Extension", each if Text.Contains([Path], ".") then Text.AfterDelimiter([Path], ".", {0, RelativePosition.FromEnd}) else "no extension", type text)

英文:

Here is an elegant way I just solved this for paths where some fines have no extension. Hope it helps someone.

= Table.AddColumn(#"AddPathColumn", "Extension", each if Text.Contains([Path], ".") then Text.AfterDelimiter([Path], ".", {0, RelativePosition.FromEnd}) else "no extension", type text)

huangapple
  • 本文由 发表于 2023年2月24日 04:33:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定