0

我有 Python 代码,我想从中提取 HTTP 参数。

在以下内容中:我怎样才能不理会这些def行,但从剩余的行中只保留可能在一行中params['...']有多个的内容:params['...']

输入:

def delete_bazooka (request):
    BType.objects.get(id=params['id']).delete()
    BType.objects.get(name=params['name']).delete()
def create_bazooka (request):
  s = Skill.objects.get(id=params['skill'])
  l = Level.objects.get(id=params['level'])
  bt = BType.objects.get(name=params['type'])
  b = Bazooka(type=qt, bin=b, bin_sequence=b.count, text=params['text'], explanation=params['chocolate'], passage=p)
def delete_hoop (request):
  h = Hoop.objects.get(id=params['id'])

期望的输出:

def delete_bazooka (request):
id
name
def create_bazooka (request):
skill
level
type
text chocolate
def delete_hoop (request):
id
4

2 回答 2

3

您可以将grep` 与 E(正则表达式)和 o(仅匹配)标志一起使用:

grep -Eo "(?def .*|'[a-z]+')" test.txt
于 2013-09-19T06:23:11.920 回答
1

使用awk

awk -F\' '{$0=/^def/?$0:$2" "$4}1' test.txt
于 2013-09-19T07:50:04.030 回答