如何使用Python从长列表名称中仅获取第一个名称,其中名称之间用点分隔。

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

How to take only frist name from long listing name with dots in between using python

问题

I have existing python file which is working as expected.

with below CODE 1, I able to return the required values,

example: I have folder name called google_com_uk (I have the multiple folder like google_com_usa, google_com_de ), with CODE 1 I'm able to take the com as a return value.

CODE 1

def services_list(root, details):
    dir = root + details
    list = glob.glob("%s/google_*" %(dir))
    no_prefix = {x.replace(f"{dir}/google_", "") for x in list}
    no_suffix = {re.sub('_.*$', '', x) for x in no_prefix}
    return no_suffix

with CODE 2, I want to achieve the same but here folder names are different

example: I have folder name called uk.google.com (I have the multiple folder like usa.google.com, de.google.com), with CODE 2, I want to take the return value as uk, nothing else!

CODE 2 - Modified as this but not able to get the required return value.

def services_list(root, details):
    dir = root + details
    list = glob.glob("%s/*.google.com" %(dir))
    no_prefix = {x.replace(f"{dir}/*.google.com", "") for x in list}
    no_suffix = {re.sub('_.*$', '', x) for x in no_prefix}
    return no_suffix
英文:

I have existing python file which is working as expected.

with below CODE 1, i able to return the required values,

example: I have folder name called google_com_uk (I have the multiple folder like google_com_usa, google_com_de ), with CODE 1 i able take the com as a return value.

CODE 1

def services_list(root, details):
dir = root + details
list = glob.glob("%s/google_*" %(dir))
no_prefix = {x.replace(f"{dir}/google_", "") for x in list}
no_suffix = {re.sub('_.*$', '', x) for x in no_prefix}
return no_suffix

with CODE 2, I want to achieve same but here folder names are different

example: I have folder name called uk.google.com (I have the multiple folder like usa.google.com, de.google.com), with CODE 2 i want to take the return value as a uk, nothing else!

CODE 2 - Modified as this but not able to get the required return value.

def services_list(root, details):
    dir = root + details
    list = glob.glob("%s/*.google.com" %(dir))
    no_prefix = {x.replace(f"{dir}/*.google.com", "") for x in list}
    no_suffix = {re.sub('_.*$', '', x) for x in no_prefix}
    return no_suffix

答案1

得分: 1

我建议使用re.search而不是删除前缀和后缀。这样,你可以定义一个正则表达式来提取你想要的特定部分。

以下是我的提议:

代码 1

import glob
import re

def services_list(root, details):
    dir = root + details
    lst = glob.glob("%s/google_*" % (dir))

    response = set()
    for el in lst:
        result = re.search(r"/google_(\w+)_(\w+)", el)

        if result is None:
            raise Exception(f"Unrecognized pattern: {el}")

        response.add(result.group(1))

    return response

同样的方法适用于另一段代码,但模式不同:

代码 2

import glob
import re

def services_list(root, details):
    dir = root + details
    lst = glob.glob("%s/google_*" % (dir))

    response = set()
    for el in lst:
        result = re.search(r"/(\w+)\.google\.(\w+)", el)

        if result is None:
            raise Exception(f"Unrecognized pattern: {el}")

        response add(result.group(1))

    return response

注意:我还建议不要使用dirlist作为变量名,因为它们是内置类。

英文:

I'd recommend using re.search instead of removing prefix and suffix. This way you can define a regex to extract the specific part that you want.

Here is my proposal:

CODE 1

import glob
import re


def services_list(root, details):
    dir = root + details
    list = glob.glob("%s/google_*" % (dir))

    response = set()
    for el in list:
        result = re.search(r"\/google_(\w+)_(\w+)", el)

        if result is None:
            raise Exception(f"Unrecognized pattern: {el}")

        response.add(result.group(1))

    return response

The same applies to the other code, but the pattern changes:

CODE 2

import glob
import re


def services_list(root, details):
    dir = root + details
    list = glob.glob("%s/google_*" % (dir))

    response = set()
    for el in list:
        result = re.search(r"\/(\w+)\.google\.(\w+)", el)

        if result is None:
            raise Exception(f"Unrecognized pattern: {el}")

        response.add(result.group(1))

    return response

Note: I also suggest NOT to use dir and list variable names because they are built-in classes

huangapple
  • 本文由 发表于 2023年3月31日 04:15:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892617.html
匿名

发表评论

匿名网友

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

确定