从降价文件中提取 SQL 个查询

Extracting SQL queries from markdown file

== Example1

Here is an example of aggregating using correlated subquery expression in projection.
This query finds the top 3 overall rated hotels.
The subquery in the projection finds the average overall rating across all rating of the given hotel document `t`.
[source,n1ql]
----
SELECT name, (SELECT raw avg(s.ratings.Overall)
              FROM   t.reviews  as s)[0] AS overall_avg_rating
FROM   `travel-sample`.inventory.hotel AS t
ORDER BY overall_avg_rating DESC
LIMIT 3;
----
.Results
[source,json]
----
  {
    "name": "Culloden House Hotel",
  },
  {
    "name": "The Bulls Head",
    "overall_avg_rating": 5
  },
  {
    "name": "La Pradella",
    "overall_avg_rating": 5
  }
]
----

I want to extract everything available in [source,n1ql] i.e the from the select statement to the limit 3; and add it to another file named example1.txt, there is such 10 to 20 example on a page.

有没有一种有效的方法可以让我用 python 做到这一点。任何可以帮助我的包裹。

根据您的模式,这应该是 python 的代码。

import re
with open("example1.md") as f1, open("example1.txt",'a') as f2:
    ft = f1.read().replace('\n',' ')
    x = re.findall('---- SELECT.*?;.*?----', ft)
    for line in x:
        f2.write(re.sub(' +', ' ', line).replace('----',' ')+'\n')