Python 3.7.3
我无法格式化结果函数的输出。例如,如果您只使用print result
,您将看到返回的所有数据。如果使用循环获取所有类型的值以标识字符串值或非字符串值:
for i in result:
print(type(result))
您将看到所有值为 str:
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
我尝试打印元组的每个位置,但它不起作用。当我尝试这样做时,我收到此错误:
print(Messages.issue_found + "ID : %s o %s" % result[0], result[1])
Traceback (most recent call last):
File "/home/sincorchetes/Projects/PyArea/sqlite/run.py", line 16, in <module>
print(Messages.issue_found + "ID : %s o %s" % result[0], result[1])
TypeError: not enough arguments for format string
这是代码:
Ops.py
import sqlite
def search_a_object(type_request):
conn = connection()
run = conn.cursor()
try:
type_request = (type_request,)
query = run.execute('SELECT * FROM Object WHERE id=?', type_request)
get_results = query.fetchone()
if get_results is None:
print("Sorry, This object was not found")
else:
#print(get_results)
return get_results
except:
print("Sorry, something was wrong :(")
run.py
import ops as Ops
import msg as Messages
request_id = input(Messages.get_request_id)
if (request_id is not None and request_id):
result = Ops.search_a_object(request_id)
print(Messages.issue_found + "ID : %s o %s" % result)
# I have problems with this line formatting ^
else:
print(Messages.need_more_data)
如果我使用代码作为up,我会得到:
Traceback (most recent call last):
File "/home/sincorchetes/Projects/PyArea/sqlite/run.py", line 16, in <module>
print(Messages.issue_found + "ID : %s o %s" % result)
TypeError: not all arguments converted during string formatting
尽管所有值都是元组内的字符串。
使用
print(Messages.issue_found + "ID : %s o %s" % (result[0], result[1]))
或
print( "ID : {} o {}".format(result[0], result[1]))