在本文中,我们将给您介绍关于Python:ValueError:太多值无法解包编辑的详细内容,并且为您解答python出现valueerror的相关问题,此外,我们还将为您提供关于OpenCVpyth
在本文中,我们将给您介绍关于Python:ValueError:太多值无法解包 编辑的详细内容,并且为您解答python出现valueerror的相关问题,此外,我们还将为您提供关于OpenCV python:ValueError:太多值无法解包、python mysql ValueError:无法处理参数、Python TypeError:无法解包不可迭代的 bool 对象、Python ValueError:太多值无法解压的知识。
本文目录一览:- Python:ValueError:太多值无法解包 编辑(python出现valueerror)
- OpenCV python:ValueError:太多值无法解包
- python mysql ValueError:无法处理参数
- Python TypeError:无法解包不可迭代的 bool 对象
- Python ValueError:太多值无法解压
Python:ValueError:太多值无法解包 编辑(python出现valueerror)
我正在编写一个opencv程序,但在另一个stackoverflow问题上找到了一个脚本: 计算机视觉掩盖人的手
运行脚本化答案时,出现以下错误:
Traceback (most recent call last): File "skinimagecontour.py", line 13, in <module> contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)ValueError: too many values to unpack
编码:
import sysimport numpyimport cv2im = cv2.imread(''Photos/test.jpg'')im_ycrcb = cv2.cvtColor(im, cv2.COLOR_BGR2YCR_CB)skin_ycrcb_mint = numpy.array((0, 133, 77))skin_ycrcb_maxt = numpy.array((255, 173, 127))skin_ycrcb = cv2.inRange(im_ycrcb, skin_ycrcb_mint, skin_ycrcb_maxt)cv2.imwrite(''Photos/output2.jpg'', skin_ycrcb) # Second imagecontours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)for i, c in enumerate(contours): area = cv2.contourArea(c) if area > 1000: cv2.drawContours(im, contours, i, (255, 0, 0), 3)cv2.imwrite(''Photos/output3.jpg'', im)
任何帮助表示赞赏!
答案1
小编典典您正在使用当前的OpenCV的master分支:return语句已更改,请参见 http://docs.opencv.org/trunk/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=cv2.findcontours#cv2.findContours。
因此,将相应的行更改为:
,等高线, = cv2.findContours(skin_ycrcb,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
或者:由于当前干线仍然不稳定,您可能还会遇到其他问题,因此您可能需要使用OpenCV的当前稳定版本2.4.9。
OpenCV python:ValueError:太多值无法解包
运行脚本答案时,出现以下错误:
Traceback (most recent call last):
File "skinimagecontour.py",line 13,in <module>
contours,_ = cv2.findContours(skin_ycrcb,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
ValueError: too many values to unpack
编码:
import sys
import numpy
import cv2
im = cv2.imread('Photos/test.jpg')
im_ycrcb = cv2.cvtColor(im,cv2.COLOR_BGR2YCR_CB)
skin_ycrcb_mint = numpy.array((0,133,77))
skin_ycrcb_maxt = numpy.array((255,173,127))
skin_ycrcb = cv2.inRange(im_ycrcb,skin_ycrcb_mint,skin_ycrcb_maxt)
cv2.imwrite('Photos/output2.jpg',skin_ycrcb) # Second image
contours,cv2.CHAIN_APPROX_SIMPLE)
for i,c in enumerate(contours):
area = cv2.contourArea(c)
if area > 1000:
cv2.drawContours(im,contours,i,(255,0),3)
cv2.imwrite('Photos/output3.jpg',im)
任何帮助表示赞赏!
python mysql ValueError:无法处理参数
如何解决python mysql ValueError:无法处理参数?
我有一些 Python 代码,可以将一列添加到表中,然后遍历列表并将每个值添加到该列中。它添加了列,但不添加任何值。它给了我错误 ValueError: 无法处理参数,但是当我在 MysqL 控制台中键入相同的命令时,它工作得很好。以下是我的代码和错误消息的相关部分。让我知道是否有任何明显的错误。请和谢谢。
mycursor = mydb.cursor()
colname=input()
que="ALTER TABLE timestamps ADD {} TIMESTAMP(6) ".format(colname)
mycursor.execute(que)
print(que)
for i in range(len(res)):
val = res[i]
print(val)
sql = "INSERT INTO timestamps ({}) VALUES (''{}'') ".format(colname,val)
print(sql)
mycursor.execute(sql,val)
mydb.commit()
asdf
ALTER TABLE timestamps ADD asdf TIMESTAMP(6)
2021-05-23 02:32:20.660
INSERT INTO timestamps (asdf) VALUES (''2021-05-23 02:32:20.660'')
Traceback (most recent call last):
File "upload_onsets.py",line 89,in <module>
mycursor.execute(sql,val)
File "/home/ubuntu2004/.local/lib/python3.8/site-packages/MysqL/connector/cursor_cext.py",line 257,in execute
prepared = self._cnx.prepare_for_MysqL(params)
File "/home/ubuntu2004/.local/lib/python3.8/site-packages/MysqL/connector/connection_cext.py",line 651,in prepare_for_MysqL
raise ValueError("Could not process parameters")
ValueError: Could not process parameters
解决方法
由于您在 cursor.execute()
中提供值,因此您需要将占位符放入 SQL,而不是将值格式化。
cursor.execute()
的第二个参数应该是一个元组,而不是单个值。
无需在循环内分配 sql
,因为它不会改变。
sql = "INSERT INTO timestamps ({}) VALUES (%s) ".format(colname)
print(sql)
for i in range(len(res)):
val = res[i]
print(val)
mycursor.execute(sql,(val,))
但是,您可以使用 executemany()
而不是循环来更有效地执行此操作。
sql = "INSERT INTO timestamps ({}) VALUES (%s) ".format(colname)
vals = [(val,) for val in res]
mycursor.executemany(sql,vals)
这会将所有插入合并到一个带有多个 VALUES
列表的查询中。
Python TypeError:无法解包不可迭代的 bool 对象
import React from 'react';
import { Header } from 'components/header/Header';
import { SEO } from 'components/seo/SEO';
import { SliceZone } from 'components/slice-zone/SliceZone';
import { client } from 'lib/prismic';
import { RichText } from 'prismic-reactjs';
const Index = ({ document }: any) => {
const slices = document;
if (!slices) return null;
return (
<>
<SEO
title={RichText.asText(document.page_title)}
description={document.meta_description}
banner={document.social_image.url}
/>
<SliceZone slices={slices} />
</>
);
};
export const getServerSideProps = async () => {
const options = {
lang: 'en-us',};
const { data: document } = await client.getSingle('homepage',options);
return {
props: {
document,},};
};
export default Index;
可以返回 3 个不同的对象。
name_ser
您需要一致的返回类型。但实际上,这个功能根本不应该存在。它试图返回 def name_ser(name):
found = False
for i in range(len(names)):
if names[i] == name:
found = True
return found,names.index(name) <== returns 2-tuple
else: <== if name[0] doesn't match,found = False executes immediately
return found <== returns boolean
<== if names is empty,returns None
或 name
中 names
的索引。 False
差不多就是这样。
str.find
在添加帐户之前,我还删除了 def main_menu():
print('Welcome!\nPlease Choose from the following options...')
print('1: Create an account\n2: Login ')
opt = int(input('Enter Your Choice: '))
if opt == 1:
name_search = input('Enter Name... ')
if name_search in names:
print("Account Already exists!")
else:
acc_creation(name_search)
print('Account created!')
为空的检查。我觉得如果只有一个人可以创建一个帐户会很奇怪。
正如之前的一些评论中提到的,您不能同时返回一个或两个值。我的建议是返回名称的索引。或者,如果未找到,则返回值 -1
。
def name_ser(name):
# Check if element is in list
if name in names:
# Return index of element
return names.index(name)
else:
# Name was not found in the list
return -1
def main_menu():
print('Welcome!\nPlease Choose from the following options...')
print('1: Create an account\n2: Login ')
opt = int(input('Enter Your Choice: '))
# Create account
if opt == 1:
name_search = input('Enter Name... ')
found = name_ser(name_search)
if found >= 0:
print("Account Already exists!")
elif found == -1 & len(names) == 0:
acc_creation(name_search)
print('Account created!')
# Login to Account
if opt == 2:
# Update to use index instead of boolean
Python ValueError:太多值无法解压
我从这段代码中得到该异常:
class Transaction:
def __init__ (self):
self.materials = {}
def add_material (self,m):
self.materials[m.type + m.purity] = m
def serialize (self):
ser_str = 'transaction_start\n'
for k,m in self.materials:
ser_str += m.serialize ()
sert += 'transaction_end\n'
return ser_str
这for
行是引发异常的行。该m
s为Material
对象。有人有什么想法吗?
今天关于Python:ValueError:太多值无法解包 编辑和python出现valueerror的介绍到此结束,谢谢您的阅读,有关OpenCV python:ValueError:太多值无法解包、python mysql ValueError:无法处理参数、Python TypeError:无法解包不可迭代的 bool 对象、Python ValueError:太多值无法解压等更多相关知识的信息可以在本站进行查询。
本文标签: