英文:
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
注意:我还建议不要使用dir
和list
作为变量名,因为它们是内置类。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论