如何用python解析(十进制(';str';),)
在我看来,我调用了一个存储过程,该存储过程会将它创建的条目的ID返回给我 我需要将此id用作另一个存储过程的参数 我的问题是第一个查询的结果是:(Decimal('1046'),),当然不适合作为第二个过程的参数 我如何解析它以仅获取“1046” 编辑: View.py:如何用python解析(十进制(';str';),),python,django,parsing,Python,Django,Parsing,在我看来,我调用了一个存储过程,该存储过程会将它创建的条目的ID返回给我 我需要将此id用作另一个存储过程的参数 我的问题是第一个查询的结果是:(Decimal('1046'),),当然不适合作为第二个过程的参数 我如何解析它以仅获取“1046” 编辑: View.py: def dictfetchall(cursor): columns = [col[0] for col in cursor.description] return [ dict(zip(colu
def dictfetchall(cursor):
columns = [col[0] for col in cursor.description]
return [
dict(zip(columns, row))
for row in cursor.fetchall()
]
def mouvementCreation(request):
idMI = 0
especes = TbEspece.objects.order_by('id')
#Get Mouvement informations
#Connection to 'erp-site' DB
cursor = connections['erp-site'].cursor()
try:
#Get Produits list from Espece
query = "{CALL SP_webGET_PRODUIT_FROM_ESPECE(%s,%s,%s,%s,%s)}"
arguments = (2016, 'C', 0, 10, 'A',)
cursor.execute(query, arguments)
produits = dictfetchall(cursor)
#Get Transporters list
cursor.execute("{CALL SP_webGET_TRANSPORT}")
transporters = dictfetchall(cursor)
#Get Livreur list
cursor.execute("{CALL SP_webGET_LIVREUR}")
livreurs = dictfetchall(cursor)
finally:
cursor.close()
cursor = connections['site'].cursor()
try:
#Get Circuit list
cursor.execute("{CALL SP_webGET_CIRCUIT_FOR_MVT}")
circuits = dictfetchall(cursor)
#Get Source list
cursor.execute("{CALL SP_webGET_SOURCE_FOR_MVT}")
mvtsources = dictfetchall(cursor)
#Get Dest list
cursor.execute("{CALL SP_webGET_DEST_FOR_MVT}")
destinations = dictfetchall(cursor)
#Get PontBascule list
cursor.execute("{CALL SP_webGET_PBASCULE}")
pontBascules = dictfetchall(cursor)
finally:
cursor.close()
reg_normes = TbRegauxnormes.objects.all()
ordreexecs = TbOrdreexecution.objects.all()
if request.method == 'POST':
typemouvement = request.POST.get('typemouvement')
soustype = request.POST.get('soustype')
recolte = request.POST.get('recolte')
groupe = request.POST.get('groupe')
categorie = request.POST.get('categorie')
code = request.POST.get('code')
collecte = request.POST.get('collecte')
vente = request.POST.get('vente')
stock = request.POST.get('stock')
achat = request.POST.get('achat')
transporteur = request.POST.get('transporteur')
blLivreur = request.POST.get('blLivreur', '')
contratClient = request.POST.get('contratClient')
pont1 = request.POST.get('pont1') # BIGINT
numTicket = request.POST.get('numTicket') # INT
dateheure1 = request.POST.get('dateheure1') # DATETIME
poid1 = request.POST.get('poid1') # INT
dsd1 = request.POST.get('dsd1') # INT
pont2 = request.POST.get('pont2') # BIGINT
dateheure2 = request.POST.get('dateheure2') # DATETIME
poid2 = request.POST.get('poid2') # INT
dsd2 = request.POST.get('dsd2') # INT
p1p2 = request.POST.get('p1p2') # INT
livreur = request.POST.get('idlivreur') # BIGINT
vehicule = request.POST.get('vehicule') # VARCHAR
comTicket = request.POST.get('comTicket') # VARCHAR
comLogiciel = request.POST.get('comLogiciel') # VARCHAR
espece = request.POST.get('espece') # BIGINT
produit = request.POST.get('produit') # BIGINT
#Connection to 'erp-site' DB
cursor = connections['pontbascule'].cursor()
try:
query = "{CALL SP_webADD_MANUAL_PESEE(%s,%s,%s,%s,%s, %s,%s,%s,%s,%s, %s,%s,%s,%s,%s,%s,%s)}"
arguments = (pont1, numTicket, dateheure1, poid1, dsd1,pont2, numTicket, dateheure2, poid2, dsd2, p1p2,livreur, vehicule,comTicket, comLogiciel,espece, produit)
cursor.execute(query, arguments)
#Here i recieve the (Decimal('1046'),)
s = cursor.fetchone()
idCreatedPesee = s[0]
finally:
cursor.close()
cursor = connections['site'].cursor()
try:
#Here im supposed to send it as argument to this procedure
query = "{CALL SP_webCREATE_MVT_INIT(%s)}"
arguments = (idCreatedPesee)
cursor.execute(query, arguments)
idCreatedMVT = dictfetchall(cursor)
finally:
cursor.close()
return render(request, 'mouvementCreation.html', {'especes' : especes, 'produits' : produits, 'transporters' : transporters, 'livreurs' : livreurs, 'circuits' : circuits, 'mvtsources' : mvtsources, 'destinations' : destinations, 'pontBascules' : pontBascules} )
python中的类型转换非常简单:
my_decimal = Decimal('1046')
as_a_string = str(my_decimal) # '1046', a string
as_an_int = int(my_decimal) # 1046, an integer
python中的类型转换非常简单:
my_decimal = Decimal('1046')
as_a_string = str(my_decimal) # '1046', a string
as_an_int = int(my_decimal) # 1046, an integer
它是
decimal.decimal
类的一个实例,是Python标准库的一部分。它准确地存储小数,这与普通浮点数不同。由于数据库执行类似的操作,将数据库的数字转换为小数是将其转换为Python的最正确的方法
在Python中,它们大多只是数字
>>> from decimal import Decimal
>>> Decimal('12.3') + 4
Decimal('16.3')
如果您知道它是一个整数,并且需要将整数传递到某个地方,请传递int(yourdecim)
使用括号和逗号获得
(十进制('1046'),)
,表示得到长度为1的元组结果。您可以使用[0]
索引它的第一个元素,就像所有元组一样。它是decimal.decimal
类的一个实例,是Python标准库的一部分。它准确地存储小数,这与普通浮点数不同。由于数据库执行类似的操作,将数据库的数字转换为小数是将其转换为Python的最正确的方法
在Python中,它们大多只是数字
>>> from decimal import Decimal
>>> Decimal('12.3') + 4
Decimal('16.3')
如果您知道它是一个整数,并且需要将整数传递到某个地方,请传递int(yourdecim)
使用括号和逗号获得
(十进制('1046'),)
,表示得到长度为1的元组结果。您可以通过使用[0]
索引它来访问它的第一个元素,就像所有元组一样。由于没有任何效果,我只在查询中使用CONVERT,以确保最终得到一个字符串。由于没有效果,我只在查询中使用CONVERT,以确保最终得到一个字符串。您能给我们实际的代码吗?我们不知道十进制是什么means@HyperNeutrino:它在标准库中@RemcoGerlich噢,哎呀,我的坏XD如果你能根据你的代码做一个例子,或者只是在发生错误的地方添加一个注释,那就太好了,所有这些代码都不会帮助你或我们找到解决方案,因为它不是很有表现力。是的,我的坏,只是添加了注释,它在视图的最底部。你能给我们你的实际代码吗?我们不知道十进制是什么means@HyperNeutrino:它在标准库中@RemcoGerlich噢,哎呀,我的坏XD如果你能根据你的代码做一个例子,或者只是在发生错误的地方添加一个注释,那就太好了,所有这些代码都不会帮助你或我们找到解决方案,因为它不是很有表现力。是的,我的坏,只是添加了注释,它位于视图的最底部。你完全正确,我首先想到了这一点,但当我尝试使用[0]访问它时,它告诉我“'Decimal'对象没有属性'items'”@Girardclément:那么不知何故,您已经有了一个Decimal对象,所以忘了最后一段。在没有看到实际代码的情况下,从这里进行猜测有点困难……我添加了my view.py,以便您可以检查它,希望它会help@Girardcl注意:您可能会将Decimal
对象的\uuu repr\uu
输出与数组或元组混淆。施法和@fixmycode的答案一样直截了当。正如我在评论中所说,我尝试了他的施法,但它不起作用。你完全正确,我首先想到了这一点,但当我尝试使用[0]访问它时,它告诉我“'Decimal'对象没有属性'items'”@Girardclément:那么你已经有一个Decimal对象了,所以忘掉最后一段吧。在没有看到实际代码的情况下,从这里进行猜测有点困难……我添加了my view.py,以便您可以检查它,希望它会help@Girardcl注意:您可能会将Decimal
对象的\uuu repr\uu
输出与数组或元组混淆。施法和@fixmycode的回答一样直截了当。正如我在评论中所说的,我尝试了他的施法,但没有效果。我试过了,得到了“str”对象没有属性“items”,就像error刚才做的一样,得到了相同的错误“str”对象没有属性“items”@Girardclément,如果是的话,然后,您的错误似乎超出了您发布的示例的范围。尝试并获得了“str”对象没有属性“items”,正如error刚才所做的,并且获得了相同的错误“'str'对象没有属性“items”@Girardclément如果是,那么您的错误似乎超出了您发布的示例的范围。