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

时间:2016-01-06 07:42:09

标签: python regex google-bigquery

我使用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,

  

[ “专题-范围-112013:Demo.Airport_Traffic”,   “专题,范围-112013:Demo.Alcohol_Consumption”   “专题,范围-112013:Demo.Flight_paths”   “专题,范围-112013:Demo.GDP_Country_Wise”   “专题,范围-112013:Demo.like_data”   “专题-范围-112013:Demo.medicare_cost”]

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

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

  

[“Airport_Traffic”,“Alcohol_Consumption”,“Flight_paths”,   “GDP_Country_Wise”,“like_data”,“medicare_cost”]

1 个答案:

答案 0 :(得分:4)

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

[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']