在角色和符号之间获取字符串,而不必关心以下符号的内容

发布于 2025-02-06 15:33:58 字数 1444 浏览 0 评论 0原文

为了将某些文件订购到文件夹中,我必须获取两个文件夹的数字(好像是某种ID)(称为px,p。Ixeced,为x一个数字,x一个数字,范围为1至200150)和文件(为px_n.gmspr,p是固定的,x是文件夹的ID号,n是文件的标识符,可以为2,3,6,8,9,a和h)。

一个示例是p.24和p24_2.gmspr,p24_3.gmspr,p24_6.gmspr,p24_8.gmspr,p24_9.gmspr,p24_a.gmspr和

p24_h.gmspr .GMSPR文件与目标文件夹PX不同。一点点OS.CHDIR和OS.RENAME和文件可以轻松移动,因此我认为这不是问题。

我想要的是获取文件名的X号码以与文件夹号进行比较,而忘记了p和_n.gmspr字符串。 而我可以通过 foldername.split(“。”,1)[1]我真的不知道该如何为文件编号做。

总而言之,我想将一些称为px_n.gmspr的文件移动到另一个文件夹几乎确定了同一PX

?谢谢你!!!

编辑: 关于给出的答案,我必须澄清自己要做的事情,特别是使用文件和文件夹格式:

Mother folder
├── Unclassified
│   └── All PX_N.gmspr being PX certain files that gotta be moved to another folders, X a number that ranges from 1 to 200150 (but not exactly 200150, is just a number ID) and N can be only 2, 3, 6, 9, A or H, nothing more. In total 15435 elements with each of the X having one of the 6 possibles N gmspr.
├──First Folder
│   └── p.X folders (X from 1 to 151), the aim is to select all the PX_N.gmspr files that agree with the X number that matches the p.X of the folder and move it to each folder.
├──Second Folder
│   └── p.X folders (X from 152 to 251, plus p.602 to p.628, p.823, p.824, 
│         p.825, p.881 and p.882)
└──Third Folder
    └── p.X folders (X from 252 to 386, plus p.585, p.586 and p. 587) 

还有其他一些文件夹来订购更多15435个文件中的一些文件夹。 我目前正在搜索Regex;对我来说不幸的是,这是我第一次真正必须使用它们。

编辑原因解决了:关键是要使用正则高音并仅获取数字,但是随着嵌套列表的出现,只有第一个数字很有用

In order to order some files into folders, I have to get the number (as if it was some sort of ID) of both folders (named as p.X, p. fixed, being X a number that can range from 1 to 200150) and files (being PX_N.gmspr, where P is fixed, X is the ID number of the folder and N an identifier of the file, which can be 2,3,6,8,9,A and H).

An example would be p.24 and P24_2.gmspr, P24_3.gmspr, P24_6.gmspr, P24_8.gmspr, P24_9.gmspr, P24_A.gmspr and P24_H.gmspr, in order to move all P24_N.gmspr to p.24

The PX_N.gmspr files are in a different folder than the target folders p.X . A little of os.chdir and os.rename and the files can be moved easily so I believe that is not a problem.

What I want is to obtain the X number of the filename to compare with the folder number, forgetting about both the P and the _N.gmspr string.
Whereas I can obtain the folder number via
foldername.split(".",1)[1] I don't really know how to do it for the file number.

To sum up, I want to move some files called PX_N.gmspr to another folder identified almost the same p.X

Any idea? Thank you!!!

EDIT:
Regarding the answer given, I have to clarify myself about what I am trying to do, specially with the file and folder format:

Mother folder
├── Unclassified
│   └── All PX_N.gmspr being PX certain files that gotta be moved to another folders, X a number that ranges from 1 to 200150 (but not exactly 200150, is just a number ID) and N can be only 2, 3, 6, 9, A or H, nothing more. In total 15435 elements with each of the X having one of the 6 possibles N gmspr.
├──First Folder
│   └── p.X folders (X from 1 to 151), the aim is to select all the PX_N.gmspr files that agree with the X number that matches the p.X of the folder and move it to each folder.
├──Second Folder
│   └── p.X folders (X from 152 to 251, plus p.602 to p.628, p.823, p.824, 
│         p.825, p.881 and p.882)
└──Third Folder
    └── p.X folders (X from 252 to 386, plus p.585, p.586 and p. 587) 

There are some other folders in order to order some more of the 15435 files.
I am currently searching about regex; unluckily for me, it is the first time I actually have to use them.

EDIT CAUSE SOLVED: SO THE POINT WAS TO PLAY WITH REGEX AND GETTING ONLY THE NUMBERS, BUT THEN AS NESTED LISTS APPEARED, ONLY THE FIRST NUMBER WAS USEFUL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻了热茶 2025-02-13 15:33:58

这是Regexes的完美工作。

首先,让我们创建一个临时的dir,并用一些文件填充以演示。

from pathlib import Path
from random import choices, randint
from string import ascii_letters

from tempfile import TemporaryDirectory

tmpdir = TemporaryDirectory()

for i in range(4):
    n = randint(1, 999)
    for i in range(randint(1, 5)):
        Path(
            tmpdir.name, f"P{n}.{''.join(choices(ascii_letters, k=10))}"
        ).touch()

现在,我们有4种类型的文件(PN。),此类文件在1到5个文件之间。

然后,我们只需要遍历这些文件,用the Regex > ) p(\ d+)\ ..+,最后创建目标dir并移动文件。

from pathlib import Path
import re

dir_re = re.compile(r"P(\d+)\..+")

for filepath in Path(tmpdir.name).iterdir():
    m = dir_re.match(filepath.name)
    dirpath = filepath.parent / f"p.{m.group(1)}"
    if not dirpath.is_dir():
        dirpath.mkdir()
    filepath.rename(dirpath / filepath.name)

例如,从平坦的临时目录中,我们现在进行了以下排序。

/var/folders/lf/z7ftpkws0vn7svq8n212czm40000gn/T/tmppve5_m1u/
├── p.413
│   └── P413.yJvxPtuzfz
├── p.705
│   ├── P705.DbwPyiFxum
│   ├── P705.FVwMuSqFms
│   ├── P705.PZyGIQEqSG
│   ├── P705.baRrkcNaZR
│   └── P705.tZKFTKwDah
├── p.794
│   ├── P794.CQTBgXOckQ
│   ├── P794.JNoKsUtgRU
│   └── P794.iSdrdohKYq
└── p.894
    └── P894.XbzFxnqYOY

最后,清理临时目录。

tmpdir.cleanup()

This is the perfect job for regexes.

First, let's create a temporary dir and fill it with some files to demonstrate.

from pathlib import Path
from random import choices, randint
from string import ascii_letters

from tempfile import TemporaryDirectory

tmpdir = TemporaryDirectory()

for i in range(4):
    n = randint(1, 999)
    for i in range(randint(1, 5)):
        Path(
            tmpdir.name, f"P{n}.{''.join(choices(ascii_letters, k=10))}"
        ).touch()

Now we have 4 types of file (PN.), with between 1 and 5 files in this type.

Then, we just need to iterate through those file, extract the N from the file name with the regex P(\d+)\..+, and finally create destination dir and move the file.

from pathlib import Path
import re

dir_re = re.compile(r"P(\d+)\..+")

for filepath in Path(tmpdir.name).iterdir():
    m = dir_re.match(filepath.name)
    dirpath = filepath.parent / f"p.{m.group(1)}"
    if not dirpath.is_dir():
        dirpath.mkdir()
    filepath.rename(dirpath / filepath.name)

For instance, from a flat temp directory, we have now the following sorted.

/var/folders/lf/z7ftpkws0vn7svq8n212czm40000gn/T/tmppve5_m1u/
├── p.413
│   └── P413.yJvxPtuzfz
├── p.705
│   ├── P705.DbwPyiFxum
│   ├── P705.FVwMuSqFms
│   ├── P705.PZyGIQEqSG
│   ├── P705.baRrkcNaZR
│   └── P705.tZKFTKwDah
├── p.794
│   ├── P794.CQTBgXOckQ
│   ├── P794.JNoKsUtgRU
│   └── P794.iSdrdohKYq
└── p.894
    └── P894.XbzFxnqYOY

And finally, cleanup the temporary directory.

tmpdir.cleanup()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文