gpt4 book ai didi

python - python中获取如下模式字符串的方法

转载 作者:行者123 更新时间:2023-11-28 22:39:34 24 4
gpt4 key购买 nike

我正在使用 tylertreat/BigQuery-Python 从 BigQuery 获取表列表下面的代码在这里,

class get_Tables:
def GET(self,r):
web.header('Access-Control-Allow-Origin', '*')
web.header('Access-Control-Allow-Credentials', 'true')
tables = []
datasetID = web.input().dataSetID
client = get_client(project_id, service_account=service_account,
private_key_file=key, readonly=True)
result = client._get_all_tables(datasetID,cache=False)
tablesWithDetails = result["tables"]
for inditable in tablesWithDetails:
tables.append(inditable["id"])
print(json.dumps(tables))
return json.dumps(tables)

上面的方法返回一个这样的JSON,

["thematic-scope-112013:Demo.Airport_Traffic", "thematic-scope-112013:Demo.Alcohol_Consumption", "thematic-scope-112013:Demo.Flight_paths", "thematic-scope-112013:Demo.GDP_Country_Wise", "thematic-scope-112013:Demo.like_data", "thematic-scope-112013:Demo.medicare_cost"]

但我只想要没有项目和数据集名称,

获取以下格式的模式或正则表达式是什么,

["Airport_Traffic", "Alcohol_Consumption", "Flight_paths", "GDP_Country_Wise", "like_data", "medicare_cost"]

最佳答案

不需要正则表达式,只需一个拆分方法就足够了。即,根据点拆分每个列表项,然后从该拆分列表中获取最后一个元素。

[i.split('.')[-1] for i in data]

示例:

>>> data =  ["thematic-scope-112013:Demo.Airport_Traffic", "thematic-scope-112013:Demo.Alcohol_Consumption", "thematic-scope-112013:Demo.Flight_paths", "thematic-scope-112013:Demo.GDP_Country_Wise", "thematic-scope-112013:Demo.like_data", "thematic-scope-112013:Demo.medicare_cost"]
>>> [i.split('.')[-1] for i in data]
['Airport_Traffic', 'Alcohol_Consumption', 'Flight_paths', 'GDP_Country_Wise', 'like_data', 'medicare_cost']

关于python - python中获取如下模式字符串的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34627955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com