Comparing text strings
jak
nospam at please.ty
Tue Apr 13 13:17:58 EDT 2021
Il 13/04/2021 01:11, Rich Shepard ha scritto:
> I'm running Slackware64-14.2 and keep a list of installed packages. When a
> package is upgraded I want to remove the earlier version, and I've not
> before written a script like this. Could there be a module or tool that
> already exists to do this? If not, which string function would be best
> suited to the task?
>
> Here's an example:
> atftp-0.7.2-x86_64-2_SBo.tgz
> atftp-0.7.4-x86_64-1_SBo.tgz
>
> and there are others like this. I want the python3 script to remove the
> first one. Tools like like 'find' or 'sort -u' won't work because while the
> file name is the same the version or build numbers differ.
>
> All suggestions welcome.
>
> Rich
>
If I understand your problem correctly, the problem would be dealing
with numbers as such in file names. This is just a track but it might
help you. This example splits filenames into strings and numbers into
tuples, appends the tuple into a list, and then sorts the list:
files = ['atftp-0.7.4-x86_64-2_SBo.tgz', 'atftp-0.7.2-x86_64-1_SBo.tgz']
digit = None
da = ''
tfn = tuple()
ltafn = list()
for f in files:
for c in f:
if str(c).isdigit():
if not digit:
if len(da) > 0:
tfn += (da,)
da = ''
digit = True
da += c
else:
da += c
else:
if digit:
if len(da) > 0:
tfn += (int(da),)
da = ''
digit = False
da += c
else:
da += c
if len(da) > 0:
if da.isdigit():
tfn += (int(da),)
else:
tfn += (da,)
da = ''
ltafn += [tfn, ]
tfn = ()
ltafn.sort()
for t in files:
print(t)
print()
for t in ltafn:
nn = ''
for f in t:
nn += str(f)
print(nn)
More information about the Python-list
mailing list