获得“真实"的干净方式Path 对象的词干? [英] Clean way to get the "true" stem of a Path object?

查看:64
本文介绍了获得“真实"的干净方式Path 对象的词干?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

预期的输入和输出:

a ->一种a.txt ->一种archive.tar.gz ->档案目录/文件 ->文件d.x.y.z/f.a.b.c ->F日志/date.log.txt ->日期#我的!

以下是我觉得很脏的实现:

<预><代码>>>>从 pathlib 导入路径>>>example_path = Path("2015 年 8 月 8 日,01'37'30.log.txt")>>>example_path.stem2015 年 8 月 8 日,01'37'30.log">>>example_path.suffixes['.log', '.txt']>>>suffixes_length = sum(map(len,example_path.suffixes))>>>true_stem = example_path.name[:-suffixes_length]>>>true_stem2015 年 8 月 8 日,01'37'30"

因为它在 Paths 没有后缀:

<预><代码>>>>ns_path = Path("no_suffix")>>>sl = sum(map(len, ns_path.suffixes))>>>ns_path.name[:-sl]''

所以我需要先检查Path是否有后缀:

<预><代码>>>>def get_true_stem(路径:路径):...如果 path.suffix:... sl = sum(map(len, path.suffixes))...返回 path.name[:-sl]... 别的:...返回 path.stem...>>>>>>get_true_stem(example_path)2015 年 8 月 8 日,01'37'30">>>get_true_stem(ns_path)无后缀"

这是我目前的用例:

<预><代码>>>>file_date = datetime.strptime(true_stem, "%B %d %Y, %H'%M'%S")>>>文件日期datetime.datetime(2015, 8, 8, 1, 37, 30)>>>new_dest = format(file_date, "%Y-%m-%dT%H:%M:%S%z") + ".log" # ISO-8601>>>Shutil.move(str(example_path), new_dest)

谢谢.

解决方案

你可以 .split 它:

<预><代码>>>>Path('logs/date.log.txt').stem.split('.')[0]'日期'

os.path 也能正常工作:

<预><代码>>>>os.path.basename('logs/date.log.txt').split('.')[0]'日期'

它通过了所有测试:

In [11]: all(Path(k).stem.split('.')[0] == v for k, v in {....: 'a': 'a',....: 'a.txt': 'a',....: 'archive.tar.gz': '存档',....: '目录/文件': '文件',....: 'd.x.y.z/f.a.b.c': 'f',....: '日志/日期.log.txt': '日期'....: }.项目())输出[11]:真

Expected inputs and outputs:

a                 -> a
a.txt             -> a
archive.tar.gz    -> archive
directory/file    -> file
d.x.y.z/f.a.b.c   -> f
logs/date.log.txt -> date # Mine!

Here's my implementation that feels dirty to me:

>>> from pathlib import Path
>>> example_path = Path("August 08 2015, 01'37'30.log.txt")
>>> example_path.stem
"August 08 2015, 01'37'30.log"
>>> example_path.suffixes
['.log', '.txt']
>>> suffixes_length = sum(map(len, example_path.suffixes))
>>> true_stem = example_path.name[:-suffixes_length]
>>> true_stem
"August 08 2015, 01'37'30"

Because it breaks on Paths without suffixes:

>>> ns_path = Path("no_suffix")
>>> sl = sum(map(len, ns_path.suffixes))
>>> ns_path.name[:-sl]
''

So I need to check if the Path has a suffix first:

>>> def get_true_stem(path: Path):
...     if path.suffix:
...         sl = sum(map(len, path.suffixes))
...         return path.name[:-sl]
...     else:
...         return path.stem
...
>>>
>>> get_true_stem(example_path)
"August 08, 2015, 01'37'30"
>>> get_true_stem(ns_path)
"no_suffix"

And this is my current use case:

>>> file_date = datetime.strptime(true_stem, "%B %d %Y, %H'%M'%S")
>>> file_date
datetime.datetime(2015, 8, 8, 1, 37, 30)
>>> new_dest = format(file_date, "%Y-%m-%dT%H:%M:%S%z") + ".log" # ISO-8601
>>> shutil.move(str(example_path), new_dest)

Thanks.

解决方案

You could just .split it:

>>> Path('logs/date.log.txt').stem.split('.')[0]
'date'

os.path works just as well:

>>> os.path.basename('logs/date.log.txt').split('.')[0]
'date'

It passes all of the tests:

In [11]: all(Path(k).stem.split('.')[0] == v for k, v in {
   ....:     'a': 'a',
   ....:     'a.txt': 'a',
   ....:     'archive.tar.gz': 'archive',
   ....:     'directory/file': 'file',
   ....:     'd.x.y.z/f.a.b.c': 'f',
   ....:     'logs/date.log.txt': 'date'
   ....: }.items())
Out[11]: True

这篇关于获得“真实"的干净方式Path 对象的词干?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆