如何将过滤器字符串加入到 url 字符串中?

问题描述 投票:0回答:1

我想将成对值(列、FILTER)列表加入到 URL 字符串中。

样本:$filter=包含(column1%2C%27FILTER1%27)+和+包含(column2%2C%27FILTER2%27)+and+包含(column3...

filter_lists = [['level', 'FIRST'], ['name', 'OFFICE'], ['number', 'FIVE']]
filter1 = f"contains({filter_lists[0][0]}%2C%27{filter_lists[0][1]}%27)"
filter_rest = [[f"+and+contains({item}%2C%27" for item in filter_lists[1:]] for subitem in filter_lists[0]][0]

filter_all = "".join(filter_rest)
url_str = f"$filter={filter1}{filter_all}"

url_str 返回: $filter=包含(level%2C%27FIRST%27)+and+contains(['name', 'OFFICE']%2C%27+and+contains(['number', 'FIVE']%2C%27

所需字符串: $filter=包含(级别%2C%27FIRST%27)+和+包含(名称%2C%27OFFICE%27)+和+包含(数量%2C%27FIVE%27)

是否有更简单的方法而不是将列和过滤器存储在列表[list]中?

python join list-comprehension
1个回答
0
投票
filter_lists = [['level', 'FIRST'], ['name', 'OFFICE'], ['number', 'FIVE']]
filter1 = f"contains({filter_lists[0][0]}%2C%27{filter_lists[0][1]}%27)"
filter_rest = [[f"+and+contains({item[0]}%2C%27{item[1]}%27)" for item in      filter_lists[1:]] for subitem in filter_lists[0]][0]

filter_all = "".join(filter_rest)
url_str = f"$filter={filter1}{filter_all}"

返回: $filter=包含(级别%2C%27FIRST%27)+和+包含(名称%2C%27OFFICE%27)+和+包含(数量%2C%27FIVE%27)

© www.soinside.com 2019 - 2024. All rights reserved.