簡體   English   中英

將python列表對象轉換為數組

[英]Convert python list object to an array

如何將 python 列表轉換為數組以包含在 mysql 查詢中? 我正在使用 mysql 連接器模塊,並收到此錯誤

Failed processing pyformat-parameters; Python 'tuple' cannot be converted to a MySQL type

我使用這種結構從 url 獲取我的值

from fastapi import FastAPI, Query
from typing import List


app = FastAPI()


@app.get("/myurl")
async def my_url(my_id: int = Query(...), my_type: List[int] = Query(None)):
query_inc = ("SELECT * from v_comments WHERE id=%(id)s and my_typeIN (%(my_type)s) ;")

我試圖使這個type=tuple(type)但是如果我不使用元組轉換我得到了下面列出的錯誤我得到了這個mysql.connector.errors.ProgrammingError: Failed processing pyformat-parameters; Python 'list' cannot be converted to a MySQL type mysql.connector.errors.ProgrammingError: Failed processing pyformat-parameters; Python 'list' cannot be converted to a MySQL type

試試這個。 如果這不起作用,請檢查您應該發送數組的格式。 (您可能必須將元素連接到一個字符串中。)

資料來源: https ://www.geeksforgeeks.org/python-convert-list-to-python-array/ 選項 1:

# Python3 code to demonstrate working of
# Convert list to Python array
# Using array() + data type indicator
from array import array

# initializing list
test_list = [6, 4, 8, 9, 10]

# printing list
print("The original list : " + str(test_list))

# Convert list to Python array
# Using array() + data type indicator
res = array("i", test_list)

# Printing result
print("List after conversion to array : " + str(res))

選項2:(如果第一次失敗)

text = ['Python', 'is', 'a', 'fun', 'programming', 'language']

# join elements of text with space
print(' '.join(text))

# Output: Python is a fun programming language

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM