Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想从 R 中的 url 中提取文件名。现在我这样做如下,但也许它可以像在 python 中那样更短。假设路径只是字符串。
path="http://www.exanple.com/foo/bar/fooXbar.xls"
在 R 中:
tail(strsplit(path,"[/]")[[1]],1)
在 Python 中:
path.split("/")[-1:]
也许一些 sub,gsub 解决方案?
有一个功能...
basename(path) [1] "fooXbar.xls"
@SimonO101 拥有最可靠的 IMO 答案,但还有其他一些选择:
由于正则表达式是贪婪的,你可以利用它来发挥你的优势
sub('.*/', '', path) # [1] "fooXbar.xls"
此外,你不应该需要[]在/你的strsplit.
[]
/
strsplit
> tail(strsplit(path,"/")[[1]],1) [1] "fooXbar.xls"