用于根据行/列表的长度创建字符串的Python函数

发布于 2025-01-11 02:18:31 字数 325 浏览 0 评论 0原文

我正在使用 Python 和 SQL Alchemy 来自动执行某些产品分类。本质上,我希望将任何带有项目的 csv 作为行,并将其格式化为 SQL 查询的 case 语句的搜索字符串。

我想用 python 编写一个函数,该函数将测量行(或列表)的长度,并在每个单词之间插入文本,并输出一个字符串,该字符串将输入到 SQL 中以查找这些单词。所附的屏幕截图是我在 Excel 中执行此操作的方式。我确信这对于具有一定串联技能的人来说是相当简单的。

脚本输出示例

I am using Python and SQL Alchemy to automate some categorizations of products. Essentially I am looking to take any csv with items as the rows and format it into search strings for a case statement of a SQL query.

I want to write a function in python that will measure the length of a row (or list) and insert text in between each of the words and output a string that will feed into SQL to find those words. The screenshot attached is how I've been doing it in excel. I'm sure this is fairly straightforward for someone with some concatenation skills.

Script Output Example

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请恋爱 2025-01-18 02:18:31

在我看来,您可以从第一列中取出值并将其拆分到一个列表中以获得您想要的结果。

例如:

input_items = [
   "Bootcut Yoga Pants",
   "Go-Go Boots",
   "Flower Print Mini Skirts",
]

for input_item in input_items:
    # Split the item in to a list of individual words
    items = input_item.split()
    # Turn the list of words in to a list of query clauses
    query_items = [f"item_name ilike '%{item}%'" for item in items]
    # Turn that in to a string, with the word "and" between each item
    query_line = " and ".join(query_items)
    # Construct the final query
    print(f"when {query_line} then '{input_item}'")

结果:

when item_name ilike '%Bootcut%' and item_name ilike '%Yoga%' and item_name ilike '%Pants%' then 'Bootcut Yoga Pants'     
when item_name ilike '%Go-Go%' and item_name ilike '%Boots%' then 'Go-Go Boots'
when item_name ilike '%Flower%' and item_name ilike '%Print%' and item_name ilike '%Mini%' and item_name ilike '%Skirts%' then 'Flower Print Mini Skirts'

Seems to me that you can take the value from your first column and split that in to a list in order to get your desired result.

For example:

input_items = [
   "Bootcut Yoga Pants",
   "Go-Go Boots",
   "Flower Print Mini Skirts",
]

for input_item in input_items:
    # Split the item in to a list of individual words
    items = input_item.split()
    # Turn the list of words in to a list of query clauses
    query_items = [f"item_name ilike '%{item}%'" for item in items]
    # Turn that in to a string, with the word "and" between each item
    query_line = " and ".join(query_items)
    # Construct the final query
    print(f"when {query_line} then '{input_item}'")

Results in:

when item_name ilike '%Bootcut%' and item_name ilike '%Yoga%' and item_name ilike '%Pants%' then 'Bootcut Yoga Pants'     
when item_name ilike '%Go-Go%' and item_name ilike '%Boots%' then 'Go-Go Boots'
when item_name ilike '%Flower%' and item_name ilike '%Print%' and item_name ilike '%Mini%' and item_name ilike '%Skirts%' then 'Flower Print Mini Skirts'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文